public async Task ReadAllAsync_Maps_Gets_And_Returns_All_Workouts_When_ApiCall_Returns_Workouts() { // Arrange var expectedWorkouts = new[] { new Workout { Name = "Test workout 01", Id = 1 }, new Workout { Name = "Test workout 02", Id = 2 }, new Workout { Name = "Test workout 03", Id = 3 } }; var expectedWorkoutsViewModel = new[] { new WorkoutViewModel { Name = "Test workout 01", Id = 1 }, new WorkoutViewModel { Name = "Test workout 02", Id = 2 }, new WorkoutViewModel { Name = "Test workout 03", Id = 3 } }; HttpClientMock .Setup(x => x.GetStringAsync(It.IsAny <string>())) .ReturnsAsync(JsonConvert.SerializeObject(expectedWorkouts)) .Verifiable(); MapperMock .Setup(x => x.Map <IEnumerable <Workout>, IEnumerable <WorkoutViewModel> >(It.IsAny <IEnumerable <Workout> >())) .Returns(expectedWorkoutsViewModel) .Verifiable(); // Act var result = await ServiceUnderTest.ReadAllAsync(); // Assert Assert.Same(expectedWorkoutsViewModel, result); MapperMock.Verify( x => x.Map <IEnumerable <Workout>, IEnumerable <WorkoutViewModel> >(It.IsAny <IEnumerable <Workout> >()), Times.Once); HttpClientMock.Verify(x => x.GetStringAsync(It.IsAny <string>()), Times.Once); }
public async void Should_Return_OkObjectResult_with_list_of_Stats() { //Arrange var expectedResult = new List <Stats>() { new Stats { StatsId = 1, Time = 10 }, new Stats { StatsId = 2, Time = 10 } }; var finalResult = new List <StatsResponse>() { new StatsResponse() { StatsId = 1, Time = 10 }, new StatsResponse() { StatsId = 2, Time = 10 } }; StatsRepository .Setup(r => r.GetAll()) .ReturnsAsync(expectedResult).Verifiable(); UnitofWorkMock .Setup(x => x.Stats) .Returns(StatsRepository.Object); MapperMock .Setup(m => m.Map <List <StatsResponse> >(expectedResult)) .Returns(finalResult).Verifiable(); //Act var result = await _uut.GetAll(); //Assert StatsRepository.Verify(); MapperMock.Verify(); var okResult = Assert.IsType <OkObjectResult>(result); Assert.Same(finalResult, okResult.Value); }
public async void Should_Return_OkObjectResult_with_list_of_leaderboards() { //Arrange var expectedResult = new List <LeaderBoard>() { new LeaderBoard { LeaderBoardId = 1 }, new LeaderBoard { LeaderBoardId = 2 } }; var finalResult = new List <LeaderBoardResponse>() { new LeaderBoardResponse() { LeaderBoardId = 1 }, new LeaderBoardResponse() { LeaderBoardId = 2 } }; LeaderBoardRepository .Setup(r => r.GetAll()) .ReturnsAsync(expectedResult).Verifiable(); UnitofWorkMock .Setup(x => x.LeaderBoard) .Returns(LeaderBoardRepository.Object); MapperMock .Setup(m => m.Map <List <LeaderBoardResponse> >(expectedResult)) .Returns(finalResult).Verifiable(); //Act var result = await _uut.GetAll(); //Assert LeaderBoardRepository.Verify(); MapperMock.Verify(); var okResult = Assert.IsType <OkObjectResult>(result); Assert.Same(finalResult, okResult.Value); }
public async void Should_Return_OkObjectResult_with_list_of_Queues() { //Arrange var expectedResult = new List <Queue>() { new Queue { QueueId = 1 }, new Queue { QueueId = 1 } }; var finalResult = new List <QueueResponse>() { new QueueResponse() { QueueId = 1 }, new QueueResponse() { QueueId = 2 } }; QueueRepositoryMock .Setup(r => r.GetAll()) .ReturnsAsync(expectedResult).Verifiable(); UnitofWorkMock .Setup(x => x.Queue) .Returns(QueueRepositoryMock.Object); MapperMock .Setup(m => m.Map <List <QueueResponse> >(expectedResult)) .Returns(finalResult).Verifiable(); //Act var result = await _uut.GetAll(); //Assert QueueRepositoryMock.Verify(); MapperMock.Verify(); var okResult = Assert.IsType <OkObjectResult>(result); Assert.Same(finalResult, okResult.Value); }
public QueryHandlersFixture() { DbSetMock.As <IQueryable <BookDomainModel> >().Setup(m => m.Provider).Returns(BookDomainModels.Provider); DbSetMock.As <IQueryable <BookDomainModel> >().Setup(m => m.Expression).Returns(BookDomainModels.Expression); DbSetMock.As <IQueryable <BookDomainModel> >().Setup(m => m.ElementType).Returns(BookDomainModels.ElementType); DbSetMock.As <IQueryable <BookDomainModel> >().Setup(m => m.GetEnumerator()) .Returns(() => BookDomainModels.GetEnumerator()); DbContextMock.Setup(c => c.Books) .Returns(DbSetMock.Object); MapperMock.Setup(_ => _.Map(It.Is <BookDomainModel>(model => model.Id == 1))) .Returns(new BookReadModel { Id = 1 }); }
protected void InitializeMocksWithEmptyCollection() { var allItems = new List <Item>().AsQueryable(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var filteredItems = new List <Item>(); var filteredItemNameDtos = new List <ItemNameDto>(); MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0))) .Returns(filteredItemNameDtos.AsQueryable()); }
public void Given_Retrieve_When_PassedValidQuery_Then_Should_ReturnTeamById() { Arena arena = Arena.Create(1, "arena", 121); var teams = TeamFactory.GetEntity("TeamTestName", "ShortName", 20, "url", arena).ToQueryableCollection(); RepositoryMock.Setup(r => r.GetTeamsByLeagueId(leagueId)).Returns(teams); var models = TeamFactory.GetModel(teams.First()).ToReadOnlyCollection(); MapperMock.Setup(m => m.Map <IReadOnlyCollection <TeamModel> >(teams)).Returns(models); var result = ExecuteQuery(); result.Should().NotBeNull(); result.Teams.Should().NotBeNull(); }
public void Given_Retrieve_When_PassedValidQuery_Then_Should_ReturnAllLeagues() { var region = Region.Create("regiontest", "www.test.ro"); var leagues = LeagueFactory.GetEntity(20, "LSTN", "LeagueFullTestName", "https://www.flagTest.png", region).ToQueryableCollection(); RepositoryMock.Setup(r => r.GetAll()).Returns(leagues); var models = LeagueFactory.GetModel(leagues.First()).ToReadOnlyCollection(); MapperMock.Setup(m => m.Map <IReadOnlyCollection <LeagueModel> >(leagues)).Returns(models); var result = ExecuteQuery(); result.Should().NotBeNull(); result.Leagues.Should().NotBeNull(); }
public void Given_Retrieve_When_PassedValidQuery_Then_Should_ReturnTeamById() { Arena arena = Arena.Create(1, "arena", 121); var team = TeamFactory.GetEntity("TeamTestName", "ShortName", 20, "url", arena); RepositoryMock.Setup(r => r.GetById(id)).Returns(team); var model = TeamFactory.GetModel(team); MapperMock.Setup(m => m.Map <TeamModel>(team)).Returns(model); var result = ExecuteQuery(); result.Should().NotBeNull(); result.Team.Should().NotBeNull(); }
public void Should_ReturnResponseWithCorrectItemsNamesOfAllItemsOfThisUser_When_SearchWordIsEmptyString() { //Arrange var allItems = BuildItemsCollection(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var firstFilteredItem = BuildFirstItem(); var secondFilteredItem = BuildSecondItem(); var thirdFilteredItem = BuildThirdItem(); var filteredItems = new List <Item>() { firstFilteredItem, secondFilteredItem, thirdFilteredItem }; var firstFilteredItemNameDto = BuildFirstItemNameDto(); var secondFilteredItemNameDto = BuildSecondItemNameDto(); var thirdFilteredItemNameDto = BuildThirdItemNameDto(); var filteredItemNameDtos = new List <ItemNameDto>() { firstFilteredItemNameDto, secondFilteredItemNameDto, thirdFilteredItemNameDto }; MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0))) .Returns(filteredItemNameDtos.AsQueryable()); //Act var result = ItemsDataService.GetItemsNames(string.Empty, "ab70793b-cec8-4eba-99f3-cbad0b1649d0"); //Assert AssertHelper.AssertAll( () => result.Data.First().Name.Should().Be("Bread"), () => result.Data.Skip(1).First().Name.Should().Be("Cheese"), () => result.Data.Skip(2).First().Name.Should().Be("Biscuits") ); }
public void Should_ReturnResponseWithCorrectItem_When_CurrentUserHasItemWithThisId() { //Arrange var allItems = BuildItemsCollection(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var filteredItem = BuildSecondItem(); var filteredItems = new List <Item>() { filteredItem }; var filteredItemDto = BuildSecondItemDto(); var filteredItemDtos = new List <ItemDto>() { filteredItemDto }; MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0))) .Returns(filteredItemDtos.AsQueryable()); //Act var result = ItemsDataService.GetItem(2, "ab70793b-cec8-4eba-99f3-cbad0b1649d0"); //Assert AssertHelper.AssertAll( () => result.Data.Id.Should().Be(2), () => result.Data.Name.Should().Be("Cheese"), () => result.Data.NextReplenishmentDate.Should().BeSameDateAs(new DateTime(2020, 10, 08)), () => result.Data.ReplenishmentPeriod.Should().Be(2) ); }
public async Task ReadOneAsync_Gets_DoesNotMap_And_Returns_Null_When_ApiCall_Returns_Null() { // Arrange const int id = 1; HttpClientMock .Setup(x => x.GetStringAsync(It.IsAny <string>())) .ReturnsAsync((string)null) .Verifiable(); MapperMock .Setup(x => x.Map <Workout, WorkoutViewModel>(It.IsAny <Workout>())) .Returns(It.IsAny <WorkoutViewModel>()) .Verifiable(); // Act var result = await ServiceUnderTest.ReadOneAsync(id); // Assert Assert.Null(result); HttpClientMock.Verify(x => x.GetStringAsync(It.IsAny <string>()), Times.Once); MapperMock.Verify(x => x.Map <Workout, WorkoutViewModel>(It.IsAny <Workout>()), Times.Never); }
public void Should_ReturnSuccessResponseWithTwoEntries_When_TwoItemsNamesOfThisUserContainSearchWord() { //Arrange var allItems = BuildItemsCollection(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var firstFilteredItem = BuildFirstItem(); var secondFilteredItem = BuildThirdItem(); var filteredItems = new List <Item>() { firstFilteredItem, secondFilteredItem }; var firstFilteredItemNameDto = BuildFirstItemNameDto(); var secondFilteredItemNameDto = BuildThirdItemNameDto(); var filteredItemNameDtos = new List <ItemNameDto>() { firstFilteredItemNameDto, secondFilteredItemNameDto }; MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemNameDto, object> >[]>(x => x.Length == 0))) .Returns(filteredItemNameDtos.AsQueryable()); //Act var result = ItemsDataService.GetItemsNames("B", "ab70793b-cec8-4eba-99f3-cbad0b1649d0"); //Assert AssertHelper.AssertAll( () => result.Data.Count().Should().Be(2), () => result.IsSuccess.Should().BeTrue(), () => result.ErrorMessage.Should().BeNull() ); }
public void Should_ReturnSuccessResponseWithItem_When_CurrentUserHasItemWithThisId() { //Arrange var allItems = BuildItemsCollection(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var filteredItem = BuildSecondItem(); var filteredItems = new List <Item>() { filteredItem }; var filteredItemDto = BuildSecondItemDto(); var filteredItemDtos = new List <ItemDto>() { filteredItemDto }; MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0))) .Returns(filteredItemDtos.AsQueryable()); //Act var result = ItemsDataService.GetItem(2, "ab70793b-cec8-4eba-99f3-cbad0b1649d0"); //Assert AssertHelper.AssertAll( () => result.Data.Should().NotBeNull(), () => result.IsSuccess.Should().BeTrue(), () => result.ErrorMessage.Should().BeNull() ); }
public void Should_ReturnErrorResponse_When_AnErrorOccursInProjectToMethod() { //Arrange var allItems = BuildItemsCollection(); ItemsRepositoryMock.Setup(x => x.All()).Returns(allItems); var filteredItem = BuildSecondItem(); var filteredItems = new List <Item>() { filteredItem }; var filteredItemDto = BuildSecondItemDto(); var filteredItemDtos = new List <ItemDto>() { filteredItemDto }; MapperMock .Setup(c => c.ProjectTo( It.Is <IQueryable <Item> >(x => ItemsTestHelper.HaveTheSameElements(filteredItems, x)), null, It.Is <Expression <Func <ItemDto, object> >[]>(x => x.Length == 0))) .Throws(new Exception()); //Act var result = ItemsDataService.GetItem(2, "ab70793b-cec8-4eba-99f3-cbad0b1649d0"); //Assert AssertHelper.AssertAll( () => result.Data.Should().BeNull(), () => result.IsSuccess.Should().BeFalse(), () => result.ErrorMessage.Should().Be("An error occured while getting item") ); }
public void Should_FirstAddItemThenCallSaveChanges_When_InputParametersAreValid() { //Arrange var itemDto = BuildFirstItemDtoWithoutId(); var item = BuildFirstItemWithoutId(); MapperMock.Setup(c => c.Map <ItemDto, Item>(itemDto)) .Returns(item); var sequence = new MockSequence(); ItemsRepositoryMock.InSequence(sequence).Setup(x => x.Add(item)); ItemsRepositoryMock.InSequence(sequence).Setup(x => x.SaveChanges()); //Act var result = ItemsDataService.AddItem("ab70793b-cec8-4eba-99f3-cbad0b1649d0", itemDto); //Assert AssertHelper.AssertAll( () => result.IsSuccess.Should().BeTrue(), () => result.ErrorMessage.Should().BeNull() ); }
public async void Should_Return_OkObjectResult_with_one_Game() { //Arrange var expectedResult = new Game() { GameId = 1 }; var finalResult = new GameResponse() { GameId = 1 }; GameRepositoryMock .Setup(x => x.Get(expectedResult.GameId)) .ReturnsAsync(expectedResult).Verifiable(); UnitofWorkMock .Setup(m => m.Game) .Returns(GameRepositoryMock.Object); MapperMock .Setup(m => m.Map <GameResponse>(expectedResult)) .Returns(finalResult); //Act var result = await _uut.Get(expectedResult.GameId); //Assert GameRepositoryMock.Verify(); MapperMock.Verify(); var okResult = Assert.IsType <OkObjectResult>(result); Assert.Same(finalResult, okResult.Value); }
public void Should_AddItemOnce_When_InputParametersAreValid() { //Arrange var itemDto = BuildFirstItemDtoWithoutId(); var item = BuildFirstItemWithoutId(); MapperMock.Setup(c => c.Map <ItemDto, Item>(itemDto)) .Returns(item); var saveObject = new Item(); ItemsRepositoryMock.Setup(c => c.Add(It.IsAny <Item>())) .Callback <Item>((obj) => saveObject = obj); ItemsRepositoryMock.Setup(c => c.SaveChanges()); //Act var result = ItemsDataService.AddItem("ab70793b-cec8-4eba-99f3-cbad0b1649d0", itemDto); //Assert ItemsRepositoryMock.Verify(c => c.Add(item), Times.Once()); }
public async void Should_return_CreatedAtActionResult_with_created_Stats() { //Arrange var expectedResult = new Stats() { Time = 30 }; var finalResult = new StatsRequest() { Time = 30 }; StatsRepository .Setup(l => l.Add(expectedResult)).Verifiable(); UnitofWorkMock .Setup(m => m.Stats) .Returns(StatsRepository.Object); MapperMock .Setup(m => m.Map <Stats>(finalResult)) .Returns(expectedResult); //Act var result = await _uut.Post(finalResult); //Assert StatsRepository.VerifyAll(); MapperMock.VerifyAll(); var response = Assert.IsType <OkObjectResult>(result); Assert.Same(finalResult, response.Value); }
public async void Should_return__error_with_message() { // Arrange var bookDto = new BookDto { Title = "Green Eggs and Ham", Rating = 8.8, YearPublished = new DateTime(1960, 1, 1), YearRead = DateTime.Now }; var book = new Book { Title = "Green Eggs and Ham", Rating = 8.8, YearPublished = new DateTime(1960, 1, 1), DateRead = DateTime.Now }; var toUpdate = MapperMock .Setup(x => x.Map <Book>(bookDto)) .Returns(book); var result = new Result(); result.AddError("there was an error"); BasicBookServiceMock .Setup(x => x.UpdateBookAsync(book)) .ReturnsAsync(result); // Act var requestResult = await ControllerUnderTest.UpdateBook(bookDto); // Assert var okResult = Assert.IsType <OkObjectResult>(requestResult); Assert.Same(result, okResult.Value); }
public async void Should_return_Throw_and_return_BadRequest_with_created_Stats() { //Arrange var expectedResult = new Stats() { Time = 30 }; var finalResult = new StatsRequest() { Time = 30 }; StatsRepository .Setup(l => l.Add(expectedResult)).Throws(new Exception()); UnitofWorkMock .Setup(m => m.Stats) .Returns(StatsRepository.Object); MapperMock .Setup(m => m.Map <Stats>(finalResult)) .Returns(expectedResult); //Act var result = await _uut.Post(finalResult); //Assert StatsRepository.VerifyAll(); MapperMock.Verify(); var ErrorResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal(400, ErrorResult.StatusCode); }
protected ParserTestsBase() { ConverterMock.Setup(m => m.Convert(It.IsAny <string>())).Verifiable(); MapperMock.Setup(m => m.MapValue(It.IsAny <object>(), It.IsAny <TValue>())).Verifiable(); ValidatorMock.Setup(m => m.Validate(It.IsAny <TValue>())).Returns(true).Verifiable(); }
public async void Should_Add_user_to_chosen_Game() { //Arrange var expectedgameId = 1; var expectedplayer1Id = 1; var expectedplayer2Id = 2; var expectedgame = new Game { GameId = expectedgameId, Players = new List <Player> { new Player { PlayerId = expectedplayer1Id }, new Player { PlayerId = expectedplayer2Id } } }; var expectedgameresponse = new GameResponse { GameId = expectedgameId, Players = new List <PlayerResponse> { new PlayerResponse { PlayerId = expectedplayer1Id }, new PlayerResponse { PlayerId = expectedplayer2Id } } }; GameRepositoryMock .Setup(x => x.AddUserToGame(expectedgameId, expectedplayer1Id, expectedplayer2Id)) .ReturnsAsync(expectedgame); UnitofWorkMock .Setup(m => m.Game) .Returns(GameRepositoryMock.Object); MapperMock .Setup(s => s.Map <GameResponse>(expectedgame)) .Returns(expectedgameresponse); //Act var result = await _uut.AddUsersToGame(expectedgameId, expectedplayer1Id, expectedplayer2Id); //Assert GameRepositoryMock.Verify(); MapperMock.Verify(); var response = Assert.IsType <OkObjectResult>(result); Assert.Same(expectedgameresponse, response.Value); }
public StoreGetByIdCommandTests() { _storeRepositoryMock.Setup(x => x.GetById(StoreWithoutAddressId)) .Returns(new Entities.StoreModule.Store { Id = StoreWithoutAddressId, Name = "Test", Phone = "1231231234", CreatedAt = DateTime.UtcNow.AddDays(-1), LastUpdatedAt = DateTime.UtcNow.AddDays(-1), Email = "*****@*****.**", Logo = "logo.jpg", TimezoneId = "testtest" }); _storeRepositoryMock.Setup(x => x.GetById(StoreWithIncorrectAddressId)) .Returns(new Entities.StoreModule.Store { Id = StoreWithIncorrectAddressId, Name = "Test-With-Address", Phone = "1231231234", CreatedAt = DateTime.UtcNow.AddDays(-1), LastUpdatedAt = DateTime.UtcNow.AddDays(-1), Email = "*****@*****.**", Logo = "logo.jpg", TimezoneId = "testtest1", AddressId = IncorrectAddressId }); _storeRepositoryMock.Setup(x => x.GetById(StoreWithAddressId)) .Returns(new Entities.StoreModule.Store { Id = StoreWithAddressId, Name = "Test-With-Address", Phone = "1231231234", CreatedAt = DateTime.UtcNow.AddDays(-1), LastUpdatedAt = DateTime.UtcNow.AddDays(-1), Email = "*****@*****.**", Logo = "logo.jpg", TimezoneId = "testtest1", AddressId = StoreAddressId }); InternalCommandServiceMock.Setup(x => x.Execute(It.Is <AddressGetByIdRequest>(y => y.Id == StoreAddressId))).Returns(new AddressGetByIdResponse { Item = new AddressDto { Street2 = "Street 2", StateOther = "STA", Zip = "12345", Street = "Street", StateId = 1, CountryId = 1, City = "City" } }); InternalCommandServiceMock.Setup(x => x.Execute(It.Is <AddressGetByIdRequest>(y => y.Id == IncorrectAddressId))).Throws <Exception>(); MapperMock.Setup(x => x.Map <Entities.StoreModule.Store, StoreDto>(It.IsAny <Entities.StoreModule.Store>())) .Returns((Entities.StoreModule.Store store) => new StoreDto { Id = store.Id, Name = store.Name, Logo = store.Logo, Email = store.Email, Phone = store.Phone }); MapperMock.Setup(x => x.Map <AddressDto, Services.StoreModule.Dtos.AddressDto>(It.IsAny <AddressDto>())) .Returns((AddressDto address) => new Services.StoreModule.Dtos.AddressDto { StateOther = address.StateOther, Street2 = address.Street2, City = address.City, Zip = address.Zip, Street = address.Street, StateId = address.StateId, CountryId = address.CountryId }); }