public async Task GetLeague_WhenExceptionIsCaught_ShouldReturnInternalServerError() { // Arrange var leagueRepository = A.Fake <ILeagueRepository>(); League?league = new League(); A.CallTo(() => leagueRepository.GetLeagueAsync(A <int> .Ignored)).Throws <Exception>(); var sharedRepository = A.Fake <ISharedRepository>(); var mapper = A.Fake <IMapper>(); LeagueModel?leagueModel = new LeagueModel(); A.CallTo(() => mapper.Map <LeagueModel>(A <League> .Ignored)).Returns(leagueModel); var linkGenerator = A.Fake <LinkGenerator>(); var testController = new LeaguesController(leagueRepository, sharedRepository, mapper, linkGenerator); int id = 1; // Act var result = await testController.GetLeague(id); // Assert result.Result.ShouldBeOfType <ObjectResult>(); ((ObjectResult)result.Result).StatusCode.ShouldBe(StatusCodes.Status500InternalServerError); ((ObjectResult)result.Result).Value.ShouldBe("Database failure"); }
public async Task GetLeague_WhenLeagueIsNotNull_ShouldReturnLeagueModelOfDesiredLeague() { // Arrange var leagueRepository = A.Fake <ILeagueRepository>(); League?league = new League(); A.CallTo(() => leagueRepository.GetLeagueAsync(A <int> .Ignored)).Returns(league); var sharedRepository = A.Fake <ISharedRepository>(); var mapper = A.Fake <IMapper>(); LeagueModel?leagueModel = new LeagueModel(); A.CallTo(() => mapper.Map <LeagueModel>(A <League> .Ignored)).Returns(leagueModel); var linkGenerator = A.Fake <LinkGenerator>(); var testController = new LeaguesController(leagueRepository, sharedRepository, mapper, linkGenerator); int id = 1; // Act var result = await testController.GetLeague(id); // Assert A.CallTo(() => leagueRepository.GetLeagueAsync(id)).MustHaveHappenedOnceExactly(); A.CallTo(() => mapper.Map <LeagueModel>(league)).MustHaveHappenedOnceExactly(); result.Value.ShouldBeOfType <LeagueModel>(); }