public async Task GetAllGroupsAsync_ShouldReturnStatusOkAndListOfGroups() { //Arrange var groupRepoMock = Substitute.For <IGroupRepository>(); groupRepoMock.GetAllAsync().Returns(new List <Group> { new Group { Id = 1, Name = "someName", Country = "someCountry", CreationYear = 2000 } }); var groupServiceMock = Substitute.For <IGroupService>(); var expected = new List <GroupDto> { GroupDto.GetGroupDtoWithId(1, "someName", "someCountry", 2000) }; groupServiceMock.GetAllAsync().Returns(expected); var sut = new GroupsController(groupServiceMock); //Act var response = await sut.GetAllGroupsAsync(); //Assert await groupServiceMock.Received().GetAllAsync(); //Assert.Equal(200, ((StatusCodeResult)response).StatusCode)); //need to fix(can`t get statusCode & value from ActionResult) but result is correct //Assert.Equal(expected, response.Value); }
public async Task GetAllGroupsAsync__An_unexpected_internal_error_occurred__Should_throw_Exception() { _groupDbServiceMock.Setup(x => x.GetAllAsync()).ThrowsAsync(new Exception()); var controller = new GroupsController(_dbServiceFactoryMock.Object, _logger, _mapperMock.Object); Func <Task> result = async() => await controller.GetAllGroupsAsync(); await result.Should().ThrowExactlyAsync <Exception>(); }
public async Task GetAllGroupsAsync__Resource_is_empty__Should_return_200OK_response_with_empty_IEnumerable() { _groupDbServiceMock.Setup(x => x.GetAllAsync()).ReturnsAsync(Enumerable.Empty <SightseeingGroup>()); _mapperMock.Setup(x => x.Map <IEnumerable <SightseeingGroupDto> >(It.IsNotNull <IEnumerable <SightseeingGroup> >())).Returns(Enumerable.Empty <SightseeingGroupDto>()); var controller = new GroupsController(_dbServiceFactoryMock.Object, _logger, _mapperMock.Object); var result = await controller.GetAllGroupsAsync(); (result as ObjectResult).StatusCode.Should().Be(200); (((result as ObjectResult).Value as ResponseWrapper).Data as IEnumerable <SightseeingGroupDto>).Should().BeEmpty(); }
public async Task GetAllGroupsAsync__An_internal_error_reffered_to_the_database_occurred__Should_throw_InternalDbServiceException() { // Example of these errors: database does not exist, table does not exist etc. _groupDbServiceMock.Setup(x => x.GetAllAsync()).ThrowsAsync(new InternalDbServiceException()); var controller = new GroupsController(_dbServiceFactoryMock.Object, _logger, _mapperMock.Object); Func <Task> result = async() => await controller.GetAllGroupsAsync(); await result.Should().ThrowExactlyAsync <InternalDbServiceException>(); }
public async Task GetAllGroupsAsync__At_least_one_element_found__Should_return_200OK_response_with_not_empty_IEnumerable() { _groupDbServiceMock.Setup(x => x.GetAllAsync()).ReturnsAsync(new SightseeingGroup[] { _validSightseeingGroup }.AsEnumerable()); _mapperMock.Setup(x => x.Map <IEnumerable <SightseeingGroupDto> >(It.IsNotNull <IEnumerable <SightseeingGroup> >())).Returns(new SightseeingGroupDto[] { _validSightseeingGroupDto }.AsEnumerable()); var controller = new GroupsController(_dbServiceFactoryMock.Object, _logger, _mapperMock.Object); var result = await controller.GetAllGroupsAsync(); (result as ObjectResult).StatusCode.Should().Be(200); (((result as ObjectResult).Value as ResponseWrapper).Data as IEnumerable <SightseeingGroupDto>).Should().NotBeEmpty(); }