예제 #1
0
            public async Task CreateAsync_Maps_Posts_And_Completes_When_ApiCall_Succeeds()
            {
                // Arrange
                var expectedWorkoutViewModel = new WorkoutViewModel {
                    Name = "Test workout 01"
                };
                var expectedWorkout = new Workout {
                    Name = "Test workout 01"
                };

                MapperMock
                .Setup(x => x.Map <WorkoutViewModel, Workout>(expectedWorkoutViewModel))
                .Returns(expectedWorkout)
                .Verifiable();
                HttpClientMock
                .Setup(x => x.PostEntityAsync(It.IsAny <string>(), expectedWorkout))
                .ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new StringContent("Content as string")
                })
                .Verifiable();

                // Act
                await ServiceUnderTest.CreateAsync(expectedWorkoutViewModel);

                // Assert
                MapperMock.Verify(x => x.Map <WorkoutViewModel, Workout>(expectedWorkoutViewModel), Times.Once);
                HttpClientMock.Verify(x => x.PostEntityAsync(It.IsAny <string>(), expectedWorkout), Times.Once);
            }
예제 #2
0
            public async Task ReadOneAsync_Gets_Maps_And_Returns_A_Workout_When_ApiCall_Returns_Workout()
            {
                // Arrange
                const int id = 1;
                var       expectedWorkout = new Workout {
                    Name = "Test workout 01", Id = id
                };
                var expectedWorkoutViewModel = new WorkoutViewModel {
                    Name = "Test workout 01", Id = id
                };

                HttpClientMock
                .Setup(x => x.GetStringAsync(It.IsAny <string>()))
                .ReturnsAsync(JsonConvert.SerializeObject(expectedWorkout))
                .Verifiable();
                MapperMock
                .Setup(x => x.Map <Workout, WorkoutViewModel>(It.IsAny <Workout>()))
                .Returns(expectedWorkoutViewModel)
                .Verifiable();

                // Act
                var result = await ServiceUnderTest.ReadOneAsync(id);

                // Assert
                Assert.Same(expectedWorkoutViewModel, result);
                HttpClientMock.Verify(x => x.GetStringAsync(It.IsAny <string>()), Times.Once);
                MapperMock.Verify(x => x.Map <Workout, WorkoutViewModel>(It.IsAny <Workout>()), Times.Once);
            }
예제 #3
0
            public async Task CreateAsync_Maps_Posts_And_Throws_HttpRequestException_When_ApiCall_Fails()
            {
                // Arrange
                var expectedWorkoutViewModel = new WorkoutViewModel {
                    Name = "Test workout 01"
                };
                var expectedWorkout = new Workout {
                    Name = "Test workout 01"
                };

                MapperMock
                .Setup(x => x.Map <WorkoutViewModel, Workout>(expectedWorkoutViewModel))
                .Returns(expectedWorkout)
                .Verifiable();
                HttpClientMock
                .Setup(x => x.PostEntityAsync(It.IsAny <string>(), expectedWorkout))
                .ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.InternalServerError,
                    Content    = new StringContent("Content as string")
                })
                .Verifiable();

                // Act, Assert
                await Assert.ThrowsAsync <HttpRequestException>(() =>
                                                                ServiceUnderTest.CreateAsync(expectedWorkoutViewModel));

                MapperMock.Verify(x => x.Map <WorkoutViewModel, Workout>(expectedWorkoutViewModel), Times.Once);
                HttpClientMock.Verify(x => x.PostEntityAsync(It.IsAny <string>(), expectedWorkout), Times.Once);
            }
예제 #4
0
        public void Should_CheckForUserIdEmptyBeforeOtherMethodCalls_When_UserIdIsEmptyString()
        {
            //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(string.Empty, itemDto);


            //Assert
            AssertHelper.AssertAll(
                () => MapperMock.Verify(x => x.Map <ItemDto, Item>(It.IsAny <ItemDto>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.Add(It.IsAny <Item>()), Times.Never()),
                () => ItemsRepositoryMock.Verify(x => x.SaveChanges(), Times.Never())
                );
        }
        public void Execute_When_called_with_valid_criteria_Then_uses_mapper_to_map_results()
        {
            var request = GetStoreFindRequest();

            Command.Execute(request);

            MapperMock.Verify(x => x.Map <IEnumerable <Entities.StoreModule.Store>, IEnumerable <StoreDto> >(_domainEntities), Times.Once);
        }
        public void Execute_When_called_with_valid_criteria_Then_uses_mapper_to_map_criteria()
        {
            var request = GetStoreFindRequest();

            Command.Execute(request);

            MapperMock.Verify(x => x.Map <StoreFindCriteriaDto, StoreFindCriteria>(request.Criteria), Times.Once);
        }
        public async Task When_Adding_Order_Then_Maps_To_Order_Dto()
        {
            // Act
            await Sut.Handle(Request, default);

            // Assert
            MapperMock.Verify(x => x.Map <OrderDto>(Order), Times.Once);
        }
예제 #8
0
        public void Execute_When_called_Then_uses_mapper_to_map_project()
        {
            Command.Execute(new StoreGetByIdRequest
            {
                Id = StoreWithoutAddressId
            });

            MapperMock.Verify(mock => mock.Map <Entities.StoreModule.Store, StoreDto>(It.Is <Entities.StoreModule.Store>(proj => proj.Id == StoreWithoutAddressId)), Times.Once);
        }
        public void PutTest()
        {
            var category = new Category {
                Name = "test"
            };

            Controller.PutAsync(category).Wait();

            MapperMock.Verify(m => m.Map <DAL.Entities.Category>(category), Times.Once());
            ServiceMock.Verify(s => s.UpdateCategoryAsync(It.IsAny <DAL.Entities.Category>()), Times.Once());
        }
        public void PutTest()
        {
            var model = new DTOs.AddOrUpdateModels.UpdateExpenseModel {
                Id = 1, Amount = 1000, CategoryId = 1, ReportId = 1, UserId = 1
            };

            Controller.PutAsync(model).Wait();

            MapperMock.Verify(m => m.Map <DAL.Entities.Expense>(model), Times.Once());
            ServiceMock.Verify(s => s.UpdateExpenseAsync(It.IsAny <DAL.Entities.Expense>()), Times.Once());
        }
예제 #11
0
            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);
            }
예제 #12
0
            public async void Should_Return_OkObjectResult_with_list_of_Games()
            {
                //Arrange
                var expectedResult = new List <Game>()
                {
                    new Game {
                        GameId = 1
                    },
                    new Game {
                        GameId = 2
                    }
                };

                var finalResult = new List <GameResponse>()
                {
                    new GameResponse()
                    {
                        GameId = 1
                    },
                    new GameResponse()
                    {
                        GameId = 2
                    }
                };

                GameRepositoryMock
                .Setup(r => r.GetAll())
                .ReturnsAsync(expectedResult).Verifiable();

                UnitofWorkMock
                .Setup(x => x.Game)
                .Returns(GameRepositoryMock.Object);

                MapperMock
                .Setup(m => m.Map <List <GameResponse> >(expectedResult))
                .Returns(finalResult).Verifiable();

                //Act
                var result = await _uut.GetAll();

                //Assert
                GameRepositoryMock.Verify();
                MapperMock.Verify();

                var okResult = Assert.IsType <OkObjectResult>(result);

                Assert.Same(finalResult, okResult.Value);
            }
예제 #13
0
            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);
            }
예제 #14
0
            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);
            }
예제 #15
0
            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);
            }
예제 #16
0
            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);
            }
예제 #17
0
            public async void Should_Return_OkObjectResult_with_one_Game()
            {
                //Arrange
                var expectedResult = new Player()
                {
                    PlayerId = 1
                };

                var finalResult = new PlayerResponse()
                {
                    PlayerId = 1
                };

                PlayerRepository
                .Setup(x => x.Get(expectedResult.PlayerId))
                .ReturnsAsync(expectedResult).Verifiable();

                UnitofWorkMock
                .Setup(m => m.Player)
                .Returns(PlayerRepository.Object);

                MapperMock
                .Setup(m => m.Map <PlayerResponse>(expectedResult))
                .Returns(finalResult);

                //Act
                var result = await _uut.Get(expectedResult.PlayerId);

                //Assert
                PlayerRepository.Verify();
                MapperMock.Verify();

                var okResult = Assert.IsType <OkObjectResult>(result);

                Assert.Same(finalResult, okResult.Value);
            }
 protected void VerifyMocks()
 {
     ConverterMock.Verify(m => m.Convert(It.IsAny <string>()), Times.Once);
     MapperMock.Verify(m => m.MapValue(It.IsAny <object>(), It.IsAny <TValue>()), Times.Once);
     ValidatorMock.Verify(m => m.Validate(It.IsAny <TValue>()), Times.Once);
 }
 public void MapperSinks()
 {
     MapperMock.Verify(m => m.MapValue(It.IsAny <object>(), It.IsAny <object>()), Times.Once);
 }
예제 #20
0
            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);
            }