Note that there are some explanatory texts on larger screens.

plurals
  1. POxUnit and Moq do not support async - await keywords
    text
    copied!<p>I am trying to discover how to apply the async and await keywords to my xUnit tests. I am using xUnit 1.9 and Async CTP 1.3. Here is my test case</p> <p>I have an interface which specifies one asynchronous method call</p> <pre><code>public interface IDoStuffAsync { Task AnAsyncMethod(string value); } </code></pre> <p>I have a class which consumes the interface and calls the async method</p> <pre><code>public class UseAnAsyncThing { private readonly IDoStuffAsync _doStuffAsync; public UseAnAsyncThing(IDoStuffAsync doStuffAsync) { _doStuffAsync = doStuffAsync; } public async Task DoThatAsyncOperation(string theValue) { await _doStuffAsync.AnAsyncMethod(theValue); } } </code></pre> <p>In my tests I wish to check that the method <code>DoThatAsyncOperation</code> is calling the method with the correct value so I mock the interface and use the Moq to verify the call</p> <pre><code> [Fact] public async void The_test_will_pass_even_though_it_should_fail() { var mock = new Mock&lt;IDoStuffAsync&gt;(); var sut = new UseAnAsyncThing(mock.Object); mock.Setup(x =&gt; x.AnAsyncMethod(It.IsAny&lt;string&gt;())); await sut.DoThatAsyncOperation("test"); // This won't throw a Moq.MockExcpetion so the test appears to pass // However it does not run mock.Verify(x =&gt; x.AnAsyncMethod("fail")); } </code></pre> <p>This test is using the <code>async</code> and <code>await</code> keywords. When it runs it erroneously passes as Moq should assert that the verify fails. Any code after the call to <code>sut.DoThatAsyncOperation("test");</code> does not run</p> <pre><code> [Fact] public void This_will_work_and_assert_the_reslt() { var mock = new Mock&lt;IDoStuffAsync&gt;(); var sut = new UseAnAsyncThing(mock.Object); mock.Setup(x =&gt; x.AnAsyncMethod(It.IsAny&lt;string&gt;())); sut.DoThatAsyncOperation("test").ContinueWith(y =&gt; { }); // This won't throw a Moq.MockExcpetion so the test appears to pass // However it does not run mock.Verify(x =&gt; x.AnAsyncMethod("fail")); } </code></pre> <p>This test is setup without the await and async keywords and passes fine.</p> <p>Is this expected behavior for xUnit and Moq? </p> <hr> <p><strong>Update</strong></p> <p>Thanks for Stephen's comment I managed to fix the first test by making two changes. The test now returns a Task instead of void and the Mock also returns a Task.</p> <pre><code> [Fact] public async Task The_test_will_pass_even_though_it_should_fail() { var mock = new Mock&lt;IDoStuffAsync&gt;(); var sut = new UseAnAsyncThing(mock.Object); mock.Setup(x =&gt; x.AnAsyncMethod(It.IsAny&lt;string&gt;())).ReturnAsync(true); await sut.DoThatAsyncOperation("test"); // This now fails as it should mock.Verify(x =&gt; x.AnAsyncMethod("fail")); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload