如何验证带有正确表达式的模拟异步方法?

时间:2019-02-21 13:46:37

标签: c# unit-testing moq xunit xunit.net

我已经使用XUnit和Moq编写了一些测试。模拟接口实现的一种方法是接受类型为Expression<Func<T, bool>>的参数,一切似乎都正常,但是我很难理解验证使用正确的表达式调用该方法的工作。

通过以下测试,即使调用看起来正确,该方法也不会返回设置中指定的值。

    /// <summary>
    /// Verify that a create fails appropriately when another entity was found given the same name,
    /// Verify that the message of the exception contains the duplicate name
    /// Verify that update fails before other calls are made
    /// </summary>
    [Theory(DisplayName = "Definition Types service - Create")]
    [MemberData(nameof(DefinitionTypesTestData.SingleDefinitionType), MemberType = typeof(DefinitionTypesTestData))]
    public async Task CreateDefinitionTypeShouldThrowDuplicateTest(DefinitionType obj)
    {
        if (obj == null) throw new NullReferenceException($"Test data was null in theory 'Definition Types service - Create'");
        var crudService = new Mock<IEntityCrudService<DefinitionType>>();
        crudService.Setup(crud => crud.GetEntitiesAsync(x => x.Name == obj.Name))
            .Returns(Task.FromResult<IEnumerable<DefinitionType>>(new List<DefinitionType> {
            new DefinitionType {
                Name = "Test",
                Description = "Test",
                DisplayName = "Test",
                ID = Guid.NewGuid()
            } }));
        IDefinitionTypesService serviceUnderTest = new DefinitionTypesService(crudService.Object);
        var exception = await Assert.ThrowsAsync<EntityDuplicationException>(() => serviceUnderTest.InsertDefinitionTypeAsync(obj));
        Assert.Contains("Definition type", exception.DisplayMessage);
        Assert.Contains(obj.Name, exception.DisplayMessage);
        crudService.Verify(crud => crud.GetEntitiesAsync(x => x.Name == obj.Name), Times.Once);
        crudService.VerifyNoOtherCalls();
    }

我对InsertDefinitionType(DefinitionType obj)具有以下实现:

async Task IDefinitionTypesService.InsertDefinitionTypeAsync(DefinitionType obj)
    {
        var definitiontypes = await _definitionTypeService.GetEntitiesAsync(x => x.Name == obj.Name);

        if(definitiontypes.Any())
        {
            throw new EntityDuplicationException("Definition type", name: obj.Name);
        }

        try
        {
            await _definitionTypeService.CreateAsync(obj);
        }
        catch (EntityNotSavedException exc)
        {
            exc.EntityType = "Definition type";
            throw exc;
        }
    }

当我如下更改设置时,我确实得到了结果,但是在我的verify函数中,它说从未调用过该函数(或至少使用给定的表达式)。 :

crudService.Setup(crud => crud.GetEntitiesAsync(It.IsAny<Expression<Func<DefinitionType, bool>>>()))
    .Returns(Task.FromResult<IEnumerable<DefinitionType>>(new List<DefinitionType> {
        new DefinitionType {
            Name = "Test",
            Description = "Test",
            DisplayName = "Test",
            ID = Guid.NewGuid()
        } }));

现在我也将验证更改为更通用:

crudService.Verify(crud => crud.GetEntitiesAsync(It.IsAny<Expression<Func<DefinitionType, bool>>>()), Times.Once);

现在我的测试通过了,但是我真的想验证方法是否正确调用而不是被完全调用。如何解决这个最简单/最好的方法?

1 个答案:

答案 0 :(得分:0)

您将需要更详细地了解设置中使用的表达式。

使用exactly one error并调用表达式进行设置和验证。

It.Is<T>()

特别注意设置和验证中使用的表达式。

[Theory(DisplayName = "Definition Types service - Create")]
[MemberData(nameof(DefinitionTypesTestData.SingleDefinitionType), MemberType = typeof(DefinitionTypesTestData))]
public async Task CreateDefinitionTypeShouldThrowDuplicateTest(DefinitionType obj) {

    if (obj == null) throw new NullReferenceException($"Test data was null in theory 'Definition Types service - Create'");

    //Arrange
    var crudService = new Mock<IEntityCrudService<DefinitionType>>();

    var list = new List<DefinitionType>() {
        new DefinitionType {
            Name = "Test",
            Description = "Test",
            DisplayName = "Test",
            ID = Guid.NewGuid()
        }
    };

    crudService
        .Setup(_ => _.GetEntitiesAsync(It.Is<Expression<Func<DefinitionType, bool>>>(exp => exp.Compile()(obj))))
        .ReturnsAsync(list);

    IDefinitionTypesService serviceUnderTest = new DefinitionTypesService(crudService.Object);

    //Act
    var exception = await Assert.ThrowsAsync<EntityDuplicationException>(() => serviceUnderTest.InsertDefinitionTypeAsync(obj));

    //Assert
    Assert.Contains("Definition type", exception.DisplayMessage);
    Assert.Contains(obj.Name, exception.DisplayMessage);
    crudService.Verify(_ => _.GetEntitiesAsync(It.Is<Expression<Func<DefinitionType, bool>>>(exp => exp.Compile()(obj))), Times.Once);
    crudService.VerifyNoOtherCalls();
}