ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
866 stars 107 forks source link

Moq's ReturnsAsync() returns the Result's Value when Result.Created(...) is used. #214

Open Cezus opened 2 weeks ago

Cezus commented 2 weeks ago

Hello,

I try to return a Result.Created(Result.Success()) via a ReturnsAsync with Moq as follows:

        var r2 = Result.Created(Result.Success());
        _mockService2
            .Setup(x => x.SyncAsync(It.IsAny<Company>(), It.IsAny<Contact>()))
            .ReturnsAsync(r2);

But when the Mock is executed in the system under test then the Value of r2 is returned. So in this case the Mocked object is returning Result.Success().

To make it clear, if I would change r2 to Result.Created(null), then a null value is returned.

On the otherhand,

        _mockService
            .Setup(x => x.SyncAsync(It.IsAny<Company>(), It.IsAny<Contact>()))
            .ReturnsAsync(Result.Error());

works like a charm. Then the Result.Error is nicely returned by the Mocked object.

I have some feeling that the implicit operators are at work with this problem but I cannot put the finger on it...

Please let me know if you need some extra clarification.