我正在进行编码的 ui 测试,基本上是 ui 的单元测试,并且我创建了一个TestObject
类,该类存储要在实例化它的 TestMethod 中针对自身执行的断言列表。
public class TestObject {
public string urlToTest;
public List<Assertion> assertions;
}
public class Assertion {
public List<SearchPropertyExpression> searchPropertyExpressions;
public Action assertMethod;
public string expectedValue; // <-- this works fine if I'll always call a method like AreEqual() where it has an expected value, but what if I want to store a method in assertMethod that has different arguments???
}
public class SearchPropertyExpression {
public string expression;
public string value;
}
我想存储 assert 方法(例如:Assert.AreEqaul(object expected, object actual)
我想对该特定方法执行TestObject
并稍后调用它,但我正在努力获得语法正确的东西。我也在努力为该委托传递参数方法(assertMethod
)实际调用时。我将调用的所有方法都在 内Microsoft.VisualStudio.TestTools.UnitTesting.Assert
。在下面的示例中,我想调用Assert.AreEqaul()
但可以调用具有不同参数的任何方法。这是我到目前为止所得到的......
[TestMethod]
public void uiTestConnectionsEducationHomePage() {
//instantiate test object
TestObject testObject = new TestObject() {
urlToTest = "/example/home.aspx",
assertions = {
new Assertion() {
searchPropertyExpressions = {
new SearchPropertyExpression() {
expression = HtmlDiv.PropertyNames.Id,
value = "header"
}
},
assertMethod = Assert.AreEqual // <-- this is wrong,I'm thinking I need to tell assertMethod what arguments to expect here, lambda??
}
}
};
// get handle to browser and launch
UiBrowserWindow uiBrowserWindow = new UiBrowserWindow();
uiBrowserWindow.launchUrl(testObject.urlToTest);
// assertions
testObject.assertions.ForEach(x => {
HtmlDiv htmlObject = new HtmlDiv();
x.searchPropertyExpressions.ForEach(p => {
htmlObject = uiBrowserWindow.uiHtmlDocument.searchHtmlElementByAttributeValue<HtmlDiv>(p.expression, p.value);
});
x.assertMethod; // <-- for this is example the arguments would be (htmlObject, "header").
});
}
我认为我真正的问题是这里有一个设计模式可以真正帮助我,但我并不精通设计模式。