简体   繁体   中英

Verify Expression<Func<T, bool>> argument in a unit test with NSubstitute

I'm working on a unit test and I'd like to verify that a mock object received the proper predicate argument. However, I fail to make it work.

Here's a skeleton of the code:

public interface IRepository<T>
{
    Task<T> SingleOrDefaultAsync(Expression<Func<T, bool>> predicate);   
}

public class MyClass
{
    private readonly IRepository<Foo> _repository

    public MyClass(IRepository<Foo> repository)
    {
        _repository = repository;
    }

    public Task<bool> MyMethod(int id)
    {
        var foo = _repository.SingleOrDefaultAsync(x => x.Id == id);

        return foo != null;
    }
}

and in my test class I have the following method

public async Task MyTestMethod()
{
    var repository = Substitute.For<IRepository<Foo>>();
    repository.SingleOrDefaultAsync(x => x.Id == 123).Returns(Task.FromResult(new Foo()));

    var myClass = new MyClass(repository);

    var result = await myClass.MyMethod(123);

    result.Should().BeTrue();
}

But as mentioned above, this test fails. I could make it pass by using Arg.Any<Expression<Func<Foo, bool>> , but it doesn't feel right.

Anyone has a suggestion what I'm doing wrong?

Capture the expression passed to the mock and use it in the Returns to verify the expected behavior.

For example

public async Task MyTestMethod() {
    //Arrange
    var id = 123;
    var model = new Foo() {
        Id = id;
    }

    var repository = Substitute.For<IRepository<Foo>>();

    repository.SingleOrDefaultAsync(Arg.Any<Expression<Func<Foo, bool>>())
        .Returns(args => {
            var expression = args.Arg<Expression<Func<Foo, bool>>(); //capture expression
            Foo result = expression.Compile()(model) ? model : null; //use to verify behavior
            Task.FromResult(result);
        });

    var myClass = new MyClass(repository);

    //Act
    var actual = await myClass.MyMethod(id);

    //Assert
    actual.Should().BeTrue();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM