2012-05-11 2 views
1

의 멤버로 저장합니다. 코드화 된 UI 테스트를 수행하고 있습니다. 기본적으로 UI에 대한 단위 테스트를 수행하고 인스턴스화하는 TestMethod 내에서 자체에 대해 수행 할 어설 션 목록을 저장하는 TestObject 클래스를 만들었습니다 그것.대리자 메서드를 클래스

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; 
} 

I (예를 들어 Assert 메서드를 저장하고 싶습니다 : 나는 특정 TestObject에 대해 실행하려는 Assert.AreEqaul(object expected, object actual)하고 나중에 전화를하지만 올바른 구문의 무언가를 얻기 위해 사투를 벌인거야 나는 또한 사투를 벌인거야. 그 대리자 메서드 (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").     
    }); 
} 

내 진짜 문제는 디자인 패턴이 정말 도움이 될 수 있지만 디자인 패턴에 정통하지 않다는 것입니다.

+0

당신은 클래스를 가지고 계십니까 당신 방법을 저장하고 싶습니까? 그것 같이 보이지 않는다 그러나 나는 나가 확실하기 위하여 검사 할 것이라는 점을 생각했다. –

+0

그래, 나는 클래스를 소유하고있다 – bflemi3

+0

좋아, 이것은 내가 당신에게 작은 데모를 줄 것이라고 생각한다. 나는 잠시만 기다려 준다. –

답변

3

사용자의 assertMethod 대리자는 반환 유형이 void이고 매개 변수가없는 메소드를 나타내는 Action 유형입니다 (예 : void Foo().
Assert.AreEqualmany overloads이며, 가장 보편적 인 것은 Assert.AreEqual(Object expected, Object actual)입니다. 나는 당신이 사용하고 그에 따라 대리자를 변경 제안 :

Action<Object, Object> assertMethod; 
+0

다니엘에게 감사드립니다. 내가 각 Assert 메서드에 적절한 인수가 전달되도록 내 TestMethod 내에서 assertMethod를 동적으로 호출하려면 어떻게해야합니까? – bflemi3

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 
} 
+0

BuggyCoder에게 감사드립니다. 문제는 대리자가 실제로 호출 될 때 인수를 전달해야하며 런타임에 실제로 호출되는 메서드가 무엇인지 알 수 없기 때문에 대리자가 호출 될 때 인수를 전달할 수 없습니다. 그게 말이 되니? 'TestObject'가 인스턴스화 될 때 인수를 지정해야한다고 생각합니다. – bflemi3

+0

assertMethod가 호출 될 때만 인수를 전달합니다. 우리가 Lambda를 사용하여 AreEquals를 작성하는 대리인 (MyAction)을 만들 때 ... lambda에서 볼 수있는 인수는 assertmethod에 정의/body .. – BuggyCoder

+0

아마 나는 틀린 각에서이 접근하고있다. 저는 현재'TestObject'에있는 모든 assertion 객체를 반복하고 각 Assertion 객체에서 assertion 메소드를 실행하려고합니다. 문제는'assertMethod'가 실제로 호출되었을 때 인자가 무엇인지를 알 수있는 방법이 없다는 것입니다. – bflemi3

관련 문제