1

我正在进行编码的 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").                   
    });
}

我认为我真正的问题是这里有一个设计模式可以真正帮助我,但我并不精通设计模式。

4

2 回答 2

3

您的assertMethod委托类型Action代表返回类型为 void 且没有参数的方法,例如void Foo().
Assert.AreEqual很多重载,最普遍的是Assert.AreEqual(Object expected, Object actual)。我建议您使用它并相应地更改您的委托:

Action<Object, Object> assertMethod;
于 2012-05-11T13:27:31.880 回答
0

如果您希望您的委托指向任何方法定义,您可以执行以下操作:-

public delegate void MyAction(params object[] args);
public class Assertion
{
    public List<PropertyExpression> propertyExpressions;
    public MyAction assertMethod;
}
public void Test()
{
   var asser = new Assertion()
                    {
                        assertMethod = (vals) =>Assert.AreEqual(vals[0],vals[1]);
                        propertyExpressions = null
                    };
   var asser2 = new Assertion()
                    {
                        assertMethod = (vals)=>Assert.AreEqual((string)vals[0],(string)vals[1],(bool)vals[2]);
                        propertyExpressions = null
                    };

    asser.assertMethod(1, 1);//calling object,object overload
    asser2.assertMethod("ab", "cd", true);//calling string,string,bool overload
}
于 2012-05-11T14:09:05.420 回答