Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created October 26, 2020 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/349a2d9a4e6c6edc9267445e68f57d6d to your computer and use it in GitHub Desktop.
Save xximjasonxx/349a2d9a4e6c6edc9267445e68f57d6d to your computer and use it in GitHub Desktop.
public interface IDataService
{
Task<List<DataModel>> GetData();
}
[ApiController]
[Route("api/Test")]
public class TestController : ControllerBase
{
private readonly IDataSerivce _dataService;
public TestController(IDataService dataService)
{
_dataService = dataService;
}
public async Task<IActionResult> Get()
{
return Ok(await _dataService.GetData());
}
}
public class given_an_instance_of_test_controller
{
[Fact]
public void assert_that_data_is_returned_from_get_call()
{
// arrange
var dataServiceMock = new Mock<IDataService>();
dataServiceMock.Setup(x => x.GetData()).ReturnsAsync(new List<DataModel> { new DataModel() } });
var controller = new TestController();
// act
var result = controller.Get().GetAwaiter().GetResult() as OkObjectResult;
// assert
var resultValue = result.Value as List<DataModel>();
Assert.NotNull(resultValue);
Assert.IsTrue(resultValue.Any());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment