ardalis / ardalis/Result

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

Open
#214 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

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(), It.IsAny()))
.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(), It.IsAny()))
.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.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the supplied Moq setup with Result.Created(Result.Success()), Result.Created(null), and Result.Error(). Inspect how ReturnsAsync resolves the implicit operators and compare the value received by the system under test. Done means the mock returns the complete Result.Created result rather than its Value, while the existing Error behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.