开发者

TypeMock Isolator, Isolate.Verify public reflective call in .NET

I want to call Isolate.Verify with a list of property, using ref开发者_运维技巧lection.

var allProps = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.DeclaredOnly);

How can I use

Isolate.Verify.WasCalledWithAnyArguments(method)

It requires an Action, all I have are PropertyInfo.


Well, this was not an easy one, but hope this answers your question! You can loop on all the properties in a given type and for each given property you perform a Verify.WasCalledWithAnyArguments.

I have written a simple unit test to demonstrate the concept. I assume there is a class TestSubject that contains the properties you want to verify were called with any arguments.

[TestMethod]
public void TestMethod1()
{
    //Arrange 
    var fake = Isolate.Fake.Instance<TestSubject>();

    //Act
    //Getting/Setting desired properties of TestSubject class 

    //Assert
    PropertyInfo[] propertyInfos =
        fake.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly |
                                        BindingFlags.SetProperty);

    foreach (var propertyInfo in propertyInfos)
    {
        Isolate.Verify.WasCalledWithAnyArguments(() => propertyInfo.GetValue(fake, null));
    }

}

Caveat: In TDD practice, there is a common tendency to consider more than one assert per unit test as a"potentially" code smell. So I encourage to try to refactor your testing code and strategy to maybe avoid the reflection.

Otherwise the aforementioned should work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜