Skip to content

Instantly share code, notes, and snippets.

@jimmymain
Forked from HEskandari/Test Async
Created November 2, 2016 08:30
Show Gist options
  • Save jimmymain/0b6b6398fa710b3c9e73a7d060cfd86c to your computer and use it in GitHub Desktop.
Save jimmymain/0b6b6398fa710b3c9e73a7d060cfd86c to your computer and use it in GitHub Desktop.
NSubstitute and Async
using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
namespace ClassLibrary1
{
public interface ICalculationServiceAsync
{
Task Calculate();
}
public class SystemUnderTest
{
private readonly ICalculationServiceAsync _service;
public SystemUnderTest(ICalculationServiceAsync service)
{
_service = service;
}
public async void DoSomethingAsync()
{
await _service.Calculate();
}
}
[TestFixture]
public class AsyncTest
{
[Test]
public void Can_await_on_async_mocks()
{
var mockService = Substitute.For<ICalculationServiceAsync>();
var sut = new SystemUnderTest(mockService);
sut.DoSomethingAsync();
mockService.Calculate().ReceivedCalls();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment