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