【问题标题】:TypeMock Isolator, Isolate.Verify public reflective call in .NETTypeMock Isolator, Isolate.Verify .NET 中的公共反射调用
【发布时间】:2011-11-30 07:04:25
【问题描述】:

我想通过属性列表调用 Isolate.Verify,使用反射。

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

如何使用

Isolate.Verify.WasCalledWithAnyArguments(method)

它需要Action,我只有PropertyInfo

【问题讨论】:

    标签: c# .net typemock-isolator


    【解决方案1】:

    嗯,这不是一件容易的事,但希望这能回答你的问题! 您可以循环给定类型中的所有属性,并为每个给定属性执行Verify.WasCalledWithAnyArguments

    我编写了一个简单的单元测试来演示这个概念。我假设有一个类 TestSubject 包含您要验证的属性是否使用任何参数调用。

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

    警告: 在 TDD 实践中,普遍倾向于将每个单元测试多个断言视为“潜在”代码异味。所以我鼓励尝试重构你的测试代码和策略来避免反射。

    否则上述应该可以工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 2011-03-13
      • 2017-08-04
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多