public void AddAuthor_WithCorrectData_ShouldAddAuthorAndReturn201Created() { // Arrange var authors = new List<Author>(); // Act var mockContext = new Mock<IMoviesGalleryData>(); mockContext.Setup(r => r.Authors).Returns(this.mockContainer.AuthorRepositoryMock.Object); var controller = new AuthorsController(mockContext.Object); this.SetupController(controller); this.mockContainer.AuthorRepositoryMock.Setup(r => r.Add(It.IsAny<Author>())) .Callback((Author author) => { authors.Add(author); }); var authorModel = new AddAuthorInputModel() { Name = "TestAuthor", DateOfBirth = new DateTime(2000, 1, 1) }; var httpPostResponse = controller.PostAuthor(authorModel).ExecuteAsync(CancellationToken.None).Result; var result = httpPostResponse.Content.ReadAsAsync<ShortActorDataModel>().Result; // Assert mockContext.Verify(c => c.SaveChanges(), Times.Once); Assert.AreEqual(1, authors.Count); Assert.AreEqual(authorModel.Name, result.Name); }
public void AddAuthor_WithInvalidData_ShouldReturn400BadRequest() { // Arrange var authors = new List<Author>(); // Act var mockContext = new Mock<IMoviesGalleryData>(); mockContext.Setup(r => r.Authors).Returns(this.mockContainer.AuthorRepositoryMock.Object); var controller = new AuthorsController(mockContext.Object); this.SetupController(controller); this.mockContainer.AuthorRepositoryMock.Setup(r => r.Add(It.IsAny<Author>())) .Callback((Author author) => { authors.Add(author); }); var httpPostResponse = controller.PostAuthor(null).ExecuteAsync(CancellationToken.None).Result; // Assert Assert.AreEqual(HttpStatusCode.BadRequest, httpPostResponse.StatusCode); Assert.AreEqual(0, authors.Count); }
public void DeleteAuthor_WithInvalidData_ShouldReturn404NotFound() { // Arrange var fakeAuthors = this.mockContainer.AuthorRepositoryMock.Object.All(); var mockContext = new Mock<IMoviesGalleryData>(); mockContext.Setup(r => r.Authors.All()).Returns(fakeAuthors.AsQueryable()); // Act var controller = new AuthorsController(mockContext.Object); this.SetupController(controller); var httpDeleteResponse = controller.DeleteAuthor(11111).ExecuteAsync(CancellationToken.None).Result; // Assert Assert.AreEqual(HttpStatusCode.NotFound, httpDeleteResponse.StatusCode); }
public void GetAuthorById_WithValidData_ShouldReturnAuthorAnd200Ok() { // Arrange var fakeAuthors = this.mockContainer.AuthorRepositoryMock.Object.All(); var mockContext = new Mock<IMoviesGalleryData>(); mockContext.Setup(r => r.Authors.All()).Returns(fakeAuthors.AsQueryable()); var author = fakeAuthors.FirstOrDefault(); // Act var controller = new AuthorsController(mockContext.Object); this.SetupController(controller); var httpResponse = controller.GetAuthorById(1).ExecuteAsync(CancellationToken.None).Result; var result = httpResponse.Content.ReadAsAsync<ShortActorDataModel>().Result; // Assert Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); Assert.AreEqual(author.Name, result.Name); }
public void DeleteAuthor_WithValidData_ShouldDeleteAuthorAndReturn200Ok() { // Arrange var fakeAuthors = this.mockContainer.AuthorRepositoryMock.Object.All(); var mockContext = new Mock<IMoviesGalleryData>(); mockContext.Setup(r => r.Authors.All()).Returns(fakeAuthors.AsQueryable()); // Act var controller = new AuthorsController(mockContext.Object); this.SetupController(controller); var httpDeleteResponse = controller.DeleteAuthor(1).ExecuteAsync(CancellationToken.None).Result; // Assert mockContext.Verify(c => c.SaveChanges(), Times.Once); Assert.AreEqual(HttpStatusCode.OK, httpDeleteResponse.StatusCode); }