Created
October 26, 2020 00:34
-
-
Save xximjasonxx/349a2d9a4e6c6edc9267445e68f57d6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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