Beispiel #1
0
        public async Task PutStarship_WhenIdDoesntMatchStarshipId_ThenReturnsBadRequestResult()
        {
            var testStarships    = GetTestStarships();
            var starshipToUpdate = testStarships.First();
            var anotherStarship  = testStarships.Last();
            var repositoryMock   = new Mock <IStarshipRepository>();

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.PutStarship(anotherStarship.Id, starshipToUpdate);

            var actionResult = Assert.IsType <BadRequestResult>(result);

            repositoryMock.Verify(x => x.UpdateStarship(starshipToUpdate), Times.Never);
        }
Beispiel #2
0
        public async Task GetStarship_WhenStarshipWithGivenIdIsNotPresentInRepository_ThenReturnsNotFoundResult()
        {
            var testStarships  = GetTestStarships();
            var id             = testStarships.First().Id;
            var repositoryMock = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.GetStarshipById(id))
            .Returns(Task.FromResult <Starship>(null));

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.GetStarship(id);

            var actionResult = Assert.IsType <ActionResult <Starship> >(result);

            Assert.IsType <NotFoundResult>(actionResult.Result);
        }
Beispiel #3
0
        public async Task PutStarship_WhenCalled_ThenUpdatesStarshipInTheRepositoryAndReturnsNoContentResult()
        {
            var testStarships    = GetTestStarships();
            var starshipToUpdate = testStarships.First();
            var repositoryMock   = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.UpdateStarship(starshipToUpdate))
            .Returns(Task.CompletedTask);

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.PutStarship(starshipToUpdate.Id, starshipToUpdate);

            var actionResult = Assert.IsType <NoContentResult>(result);

            repositoryMock.Verify(x => x.UpdateStarship(starshipToUpdate), Times.Once);
        }
Beispiel #4
0
        public async Task GetStarships_WhenNoStarshipsAreInRepository_ThenReturnsEmptyCollection()
        {
            var repositoryMock = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.GetAllStarships())
            .Returns(Task.FromResult(Enumerable.Empty <Starship>()));
            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.GetStarships();

            Assert.IsType <OkObjectResult>(result.Result);

            var okResult = result.Result as OkObjectResult;

            var items = Assert.IsAssignableFrom <IEnumerable <Starship> >(okResult.Value);

            items.Should().HaveCount(0);
        }
Beispiel #5
0
        public async Task DeleteStarship_WhenCalledWithStarshipIdThatDoesNotExistsInRepository_ThenReturnsNotFoundResult()
        {
            var testStarships    = GetTestStarships();
            var starshipToDelete = testStarships.First();
            var repositoryMock   = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.GetStarshipById(starshipToDelete.Id))
            .Returns(Task.FromResult <Starship>(null));
            repositoryMock.Setup(x => x.RemoveStarship(starshipToDelete))
            .Returns(Task.CompletedTask);

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.DeleteStarship(starshipToDelete.Id);

            Assert.IsType <NotFoundResult>(result.Result);
            repositoryMock.Verify(x => x.RemoveStarship(starshipToDelete), Times.Never);
        }
Beispiel #6
0
        public async Task PostStarship_WhenCalled_ThenAddsStarshipToTheRepositoryAndReturnsCreatedAtActionResult()
        {
            var testStarships  = GetTestStarships();
            var starshipToAdd  = testStarships.First();
            var repositoryMock = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.AddStarship(starshipToAdd))
            .Returns(Task.CompletedTask);

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.PostStarship(starshipToAdd);

            var actionResult = Assert.IsType <CreatedAtActionResult>(result.Result);

            actionResult.Value.Should().Be(starshipToAdd);
            repositoryMock.Verify(x => x.AddStarship(starshipToAdd), Times.Once);
        }
Beispiel #7
0
        public async Task GetStarship_WhenStarshipWithGivenIdIsPresentInRepository_ThenReturnsThatStarship()
        {
            var testStarships  = GetTestStarships();
            var id             = testStarships.First().Id;
            var repositoryMock = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.GetStarshipById(id))
            .Returns(Task.FromResult(testStarships.First()));

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.GetStarship(id);

            Assert.IsType <OkObjectResult>(result.Result);

            var okResult = result.Result as OkObjectResult;

            var item = Assert.IsAssignableFrom <Starship>(okResult.Value);

            item.Should().Be(testStarships.First());
        }
Beispiel #8
0
        public async Task DeleteStarship_WhenCalled_ThenRemovesStarshipFromRepositoryAndReturnsIt()
        {
            var testStarships    = GetTestStarships();
            var starshipToDelete = testStarships.First();
            var repositoryMock   = new Mock <IStarshipRepository>();

            repositoryMock.Setup(x => x.GetStarshipById(starshipToDelete.Id))
            .Returns(Task.FromResult(starshipToDelete));
            repositoryMock.Setup(x => x.RemoveStarship(starshipToDelete))
            .Returns(Task.CompletedTask);

            var sut = new StarshipsController(repositoryMock.Object);

            var result = await sut.DeleteStarship(starshipToDelete.Id);

            Assert.IsType <OkObjectResult>(result.Result);

            var okResult = result.Result as OkObjectResult;

            var item = Assert.IsAssignableFrom <Starship>(okResult.Value);

            item.Should().Be(starshipToDelete);
            repositoryMock.Verify(x => x.RemoveStarship(starshipToDelete), Times.Once);
        }
 public void Setup()
 {
     this.controller = new StarshipsController(this.starshipRepositoryMock.Object, this.loggerMock.Object);
 }