4

我正在尝试测试一个需要工厂(Func<T>)的类,并且我正在使用 Moq 和 AutoFixture。

设置“环境”以查看工厂是否已使用以及在返回的实例上使用了多少次以及使用了哪些方法的最佳方法是什么?

目前我正在模拟T和计算所有返回的模拟实例InjectingFunc<T>

public class SystemUnderTest {
    public SystemUnderTest(Func<IMyClass> c)
    {
        try {
            var c1 = c();
            c1.Name="n1";
            c1.Send();
        }
        catch(Exception){
            var c2 = c();
            c2.Name="n2";
            c2.Send();
        }
    }
}
private Mock<IMyClass> MockFactory()
{
   var m = new Mock<IMyClass>();
   m.SetupProperty(mc=>mc.Name);
   _returnedStubs.Add(m);
   return m;
}  
[Test]
public void TestSomething()
{
    var fixture = new Fixture();
    fixture.Inject(()=>MockFactory().Object)
    var sut = fixture.CreateAnonymous<SystemUnderTest>();
    Assert.That(_returnedStubs.Count,Is.Equal(1));
    _returnedStubs[0].Verify(m=>m.Send(),Times.Exactly(1));
    _returnedStubs[0].Verify(m=>m.Name = "n1");
}

但这对我来说有点可疑/丑陋。而且我很确定测试类中的实例变量是一件危险的事情

4

2 回答 2

7

由于 AutoFixture能够创建匿名委托,因此当被要求创建 的匿名实例时SystemUnderTest,它还会自动提供一个匿名委托,该委托又会在调用时Func<IMyClass>返回一个匿名实例。IMyClass

这意味着,在这种情况下:

public class SystemUnderTest
{
    public SystemUnderTest(Func<IMyClass> c)
    {
        try
        {
            var c1 = c();
            // ...
        }
        catch (Exception)
        {
            var c2 = c();
            // ...
        }
    }
}

以下代码:

var fixture = new Fixture();
var sut = fixture.CreateAnonymous<SystemUnderTest>();

将使用 的匿名实例分配c1和变量。此外,如果您将 AutoFixture 配置为自动模拟容器,例如使用,则这些匿名实例也将恰好是 Moq 代理:c2IMyClassAutoMoqCustomizationIMyClass

var fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization());
var sut = fixture.CreateAnonymous<SystemUnderTest>();

然而,这些信息虽然有用,但在您的特定情况下并不能真正帮助您,因为您需要在测试中获取工厂方法返回的模拟对象Func<IMyClass>,以便配置它们的行为并对它们如何做出一些断言'已经互动。

在我看来,最好的解决方案是将工厂方法的实现Func<IMyClass>更改为接口。通过这种方式,您可以创建一个假工厂,该工厂在按顺序多次调用该方法时返回不同IMyClass接口模拟:Create

所以,给定这个例子:

public interface IFactory<T>
{
    T Create();
}

public class SystemUnderTest
{
    public SystemUnderTest(IFactory<IMyClass> factory)
    {
        try
        {
            var c1 = factory.Create();
            // ...
        }
        catch (Exception)
        {
            var c2 = factory.Create();
            // ...
        }
    }
}

您可以按如下方式设置测试场景:

    // Given
    var c1 = new Mock<IMyClass>();
    var c2 = new Mock<IMyClass>();
    // TODO: setup the behavior of the mock objects
    var factory = new Mock<IFactory<IMyClass>>();
    factory.Setup(s => s.Create()).ReturnsInOrder(c1.Object, c2.Object);

    // When
    var fixture = new Fixture();
    fixture.Inject(() => factory.Object)
    var sut = fixture.CreateAnonymous<SystemUnderTest>();

    // Then
    // TODO: verify the expectations on the mock objects

请注意,这ReturnsInOrder是一个自定义扩展方法Callback,当它被连续多次调用时,它使用 Moq 中的方法从存根方法返回不同的值。

于 2012-03-24T00:45:20.047 回答
1

最好的方法之一是创建自己的函数并传递它,然后在那里设置期望或状态。

int numberOfTimesUsed = 0;
myObject.Func = (x) => 
{ 
   numberOfTimesUsed++;
   Assert.IsNotNull(x); // checks if x passed was not null
};

...

Assert.AreEqual(2, numberOfTimesUsed); // checks if called twice
于 2012-03-23T12:59:19.040 回答