如何验证调用方法?

时间:2011-11-09 17:40:20

标签: c# unit-testing moq autofixture

我有一个ICreateService类,它依赖于ITicketApiAdapter。我已经尝试注册一个模拟ITicketAdaper,以便在创建匿名创建服务时注入它。

所以,在设置中,我有这个票证适配器的注册表:

Fixture.Register(() =>
    {
        var ticketApiAdapter = new Mock<ITicketApiAdapter>();
        ticketApiAdapter
            .Setup( x => x.AddTicketComment(
                It.IsAny<User>(), 
                It.IsAny<Customer>(), 
                It.IsAny<TicketComment>()))
        .Returns(new SaveResult
                        {
                            Success = true, 
                            Id = Fixture.CreateAnonymous<Guid>().ToString()
                        });

        return ticketApiAdapter;
    });

Fixture.Register(() => new CreateService(Fixture.CreateAnonymous<Mock<ITicketApiAdapter>>().Object));

根据我的理解,这应该“冻结”ICreateServiceMock<ITicketApiAdapter>,这样当我请求匿名实例时,它就是我注册的那个。

我有一个看起来像这样的测试:

[TestMethod]
public void CreateServiceCallsAddTicketComment()
{
    var apiTicketAdapter = Fixture.CreateAnonymous<Mock<ITicketApiAdapter>>();
    var createTicketRequest = Fixture.CreateAnonymous<CreateTicketComment>();
    var createService = Fixture.CreateAnonymous<CreateService>();

    var results = createService.CreateTicketComment(createTicketRequest);

    apiTicketAdapter
        .Verify(x => x.AddTicketComment(
            It.IsAny<User>(), 
            It.IsAny<Customer>(), 
            It.IsAny<TicketComment>()), 
        Times.Once());

    Assert.IsTrue(results.All(x => x.Success));
    Assert.IsTrue(results.All(x => x.Errors.Count == 0));

}

我希望apiTicketAdapter是我注册的那个,以便我可以验证该方法是否被调用。如果我单步执行,则会调用TicketApiAdapter,但是Moq说它不是。

修改

这是我得到的错误:

  

CreateServiceCallsAddTicketComment抛出异常:   Moq.MockException:模拟一次的预期调用,但为0   时间:x =&gt; x.AddTicketComment(It.IsAny(),It.IsAny(),   It.IsAny())

     

已配置的设置:x =&gt; x.AddTicketComment(It.IsAny(),   It.IsAny(),It.IsAny()),Times.Never No   调用。

1 个答案:

答案 0 :(得分:4)

当您注册代码块时,每次 Fixture实例解析所请求的类型时,将调用该代码块。这意味着它已冻结。如果你想冻结一些东西,Freeze overloads之一通常更容易使用。

更好的是,既然你似乎在使用Moq,我建议你使用AutoMoq extension吗?

这将使您能够将测试重写为以下内容:

[TestMethod]
public void CreateServiceCallsAddTicketComment(new AutoMoqCustomization());
{
    var fixture = new Fixture().Customize()
    var apiTicketAdapter = fixture.Freeze<Mock<ITicketApiAdapter>>();
    ticketApiAdapter
        .Setup( x => x.AddTicketComment(
            It.IsAny<User>(), 
            It.IsAny<Customer>(), 
            It.IsAny<TicketComment>()))
        .Returns(new SaveResult
        {
            Success = true, 
            Id = Fixture.CreateAnonymous<Guid>().ToString()
        });

    var createTicketRequest = fixture.Freeze<CreateTicketComment>();
    var createService = fixture.CreateAnonymous<CreateService>();

    var results = createService.CreateTicketComment(createTicketRequest);

    apiTicketAdapter
        .Verify(x => x.AddTicketComment(
            It.IsAny<User>(), 
            It.IsAny<Customer>(), 
            It.IsAny<TicketComment>()), 
        Times.Once());

    Assert.IsTrue(results.All(x => x.Success));
    Assert.IsTrue(results.All(x => x.Errors.Count == 0));

}

假设CreateTicketRequest使用构造函数注入或属性注入。