SomeMethod(Func> action, bool ignore) " /> SomeMethod(Func> action, bool ignore) " /> SomeMethod(Func> action, bool ignore) "/>

How to mock Func<Task<generic type>> in c#

60 Views Asked by At

I have make a generic class like "GenericProxy : IProxy" and method like this :

public async Task<T> SomeMethod<T>(Func<Task<T>> action, bool ignore) {
        if (ignore) {
            return await action();
        }

        return await proxy<T>.ExecuteAsync(_ => action());
}

To apply this, I make a code like this :

public async Task<string> StoreBook(string bookName) {
    var findBookResult = await proxy.SomeMethod<string>(async () => await BorrowBookProcess(bookName), false);
    return findBookResult;
}

public async Task<string> BorrowBookProcess (string bookName) {
    // do something
    return await bookRepository.findbook(bookName);
}

In the unit test, and the below test method is working

[Fact]
public async void SomeMethod_return_AsExpected() {
    Mock<IProxy> mockPoxy = new Mock<IProxy>();
    string assertResult = "Finded";

    mockPoxy.Setup(
                x => x.SomeMethod(
                    It.IsAny<Func<Task<string>>>(),
                    false))
            .ReturnsAsync(assertResult)
            .Verifiable();

    var getReturn = sut.StoreBook(assertResult);

    assert.equal(getReturn, assertResult);
}

but in some reason, I want to mock bookRepository and return value where not only mock IProxy , do anyone advise how to do it ?

Thank you

0

There are 0 best solutions below