模拟Linq“任何”与Moq的谓词

时间:2016-08-09 14:04:04

标签: c# linq unit-testing mocking moq

我正在尝试使用以下代码在我的存储库中模拟AnyAsync方法,但存储库始终返回false

AnyAsync的签名是:

Task<bool> AnyAsync<TEntity>(Expression<Func<TEntiry, bool>> predicate)

我尝试了以下设置:

1:

mockCustomerRepository.Setup(r => r.AnyAsync(c => c.Email == "some@one.com"))
   .ReturnsAsync(true);

2:

Expression<Func<CustomerEntity, bool>> predicate = expr => 
    expr.CustomerPerson.Email == "some@one.com";

mockCustomerRepository.Setup(r => r.AnyAsync(It.Is<Expression<Func<CustomerEntity, bool>>>
   (criteria => criteria == predicate))).ReturnsAsync(true);

3:

mockCustomerRepository.Setup(r => r.AnyAsync(It.IsAny<Expression<Func<CustomerEntity, bool>>>()))
    .ReturnsAsync(true);

我的测试:

public class Test 
{
    Mock<ICustomerRepository> mockCustomerRepository;

    public Test()
    {
        mockCustomerRepository = new Mock<ICustomerRepository>();
    }

    [Fact]
    public async Task CustomerTest()
    {   
        var customer = ObjectFactory.CreateCustomer(email: "some@one.com");
        var sut = new CustomerService(mockCustomerRepository.Object);

        var result = await sut.ValidateCustomerAsync(customer);
        .
        .
        .
    }
}

我的CustomerService.ValidateCustomerAsync方法:

public async Task<OperationResult> ValidateCustomerAsync(CustomerEntity customer)
{
    var errors = new List<ValidationResult>();

    if (await _repository.AnyAsync(c => c.Email == customer.Email))
        errors.Add(new ValidationResult("blah blah")));

我也读过this,但它也不起作用。

4 个答案:

答案 0 :(得分:5)

以下代码段显示了模拟AnyAsync方法的正确方法:

[TestMethod]
public async Task TestMethod1()
{
    var fakeCustomerRepo = new Mock<ICustomerRepository>();
    var foo = false;
    fakeCustomerRepo.Setup(repository => repository.AnyAsync(It.IsAny<Expression<Func<CustomerEntity, bool>>>()))
        .Callback<Expression<Func<CustomerEntity, bool>>>(
            expression =>
            {
                var func = expression.Compile();
                foo = func(new CustomerEntity() {Email = "foo@gmail.com"});
            })
        .Returns(() => Task.FromResult(foo));

    var customer = new CustomerEntity() {Email = "foo@gmail.com"};
    var result = await fakeCustomerRepo.Object.AnyAsync<CustomerEntity>(c => c.Email == customer.Email);
    Assert.IsTrue(result);

    customer = new CustomerEntity() { Email = "boo@gmail.com" };
    result = await fakeCustomerRepo.Object.AnyAsync<CustomerEntity>(c => c.Email == customer.Email);
    Assert.IsFalse(result);
}

使用上面的设置,您可以验证谓词,这是您单位行为的一部分。

答案 1 :(得分:1)

我认为你正在遇到匹配谓词的困难。 Func的Func或表达式使用引用相等,因此仅使用==来比较这两个实例是不行的。 (作为一般规则,如果你不能让predicate1.Equals(predicate2)返回true,那么Moq的论证匹配将无法匹配。)

这有点不正统,但我推荐你my answer to a similar question for FakeItEasy matchers,这反过来又指向了许多用于验证谓词的技巧。

答案 2 :(得分:0)

我只能看到应该导致此问题的两个选项:

1。存储库方法不是来自您应该模拟返回所需值的接口,因为它没有标记为virtual

2。使用moq时,方法中使用的类型(TEntity)不匹配。

AnyAsync<TEntity>(Expression<Func<TEntity, bool>> 

您可以设置为MemberEntity,然后使用ValidateCustomerAsync致电CustomerEntity

答案 3 :(得分:0)

也许我错了但是,但据我读过只返回任务的方法,.Returns(Task.FromResult(default(object)))可以而且应该使用。

所以在你的情况下,它将是mocksCustomerRepository.Setup(r => r.AnyAsync(c => c.Email == "some@one.com")).Returns(Task.FromResult(true));