Skip to content

Instantly share code, notes, and snippets.

@HEskandari
Created October 28, 2013 07:31
Show Gist options
  • Save HEskandari/7192698 to your computer and use it in GitHub Desktop.
Save HEskandari/7192698 to your computer and use it in GitHub Desktop.
[TestFixture]
public class MockingAsyncTask
{
[Test]
public async void can_mock_interface_methods_with_task_return()
{
var mock = NSubstitute.Substitute.For<ILongRunningOperation>();
var sut = new SystemUnderTest {Operaiton = mock};
await sut.Execute();
mock.DoSomething().ReceivedCalls();
}
public class SystemUnderTest
{
public ILongRunningOperation Operaiton { get; set; }
public async Task Execute()
{
await Operaiton.DoSomething();
}
}
public interface ILongRunningOperation
{
Task DoSomething();
}
}
@JakeGinnivan
Copy link

https://gist.github.com/HEskandari/7192698#file-mockingasynctask-cs-L12

mock.DoSomething().ReceivedCalls(); is not right mock.DoSomething() returns a task, then you are calling .ReceivedCalls() on a task which is not a substitute.

mock.ReceivedCalls(); should return one thing, which is mock.DoSomething()

@shiftkey
Copy link

shiftkey commented Jan 3, 2014

mock.Received().DoSomething(); is what you should be using to assert that a specific method was called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment