public void GivenIsValidIsbn_WhenIsbnIsValid_ThenShouldReturnTrue() { // Arrange var isbn = BookStub.CreateNew().Isbn; var repository = new Mock <IRepository <Book> >(); var service = new BookService(repository.Object); // Act var result = service.IsValidIsbn(isbn); // Assert result.Should().BeTrue(); }
public void GivenCreate_WhenInvokedWithBook_ThenShouldAddBookToTheDbSet() { // Arrange var book = BookStub.CreateNew(); var set = new Mock <DbSet <Book> >(); var context = new Mock <DbContext>(); context.Setup(c => c.Set <Book>()).Returns(set.Object); var repository = new BookRepository(context.Object); // Act repository.Create(book); // Assert set.Verify(s => s.Add(It.IsAny <Book>()), Times.Exactly(1)); }
public void GivenHttpGetVerb_WhenRequestParameterIdentifiesAnExistingBook_ThenShouldReturnStatusOkAndTheBook() { // Arrange var book = BookStub.CreateNew(); var service = new Mock <IBookService>(); service.Setup(s => s.RetrieveByIsbn(book.Isbn)).Returns(book); var controller = new BookController(service.Object); // Act var result = controller.Get(book.Isbn) as OkNegotiatedContentResult <Book>; // Assert service.Verify(s => s.RetrieveByIsbn(It.IsAny <string>()), Times.Exactly(1)); result.Should().BeOfType <OkNegotiatedContentResult <Book> >(); result.Content.ShouldBeEquivalentTo(book); }
public void GivenHttpPostVerb_WhenRequestBodyIsBookWithInvalidIsbn_ThenShouldReturnStatusBadRequest() { // Arrange var book = BookStub.CreateNew(); book.Isbn = null; var service = new Mock <IBookService>(); service.Setup(s => s.Create(book)); var controller = new BookController(service.Object); // Act var result = controller.Post(book) as BadRequestResult; // Assert service.Verify(s => s.Create(It.IsAny <Book>()), Times.Never); result.Should().BeOfType <BadRequestResult>(); }
public void GivenHttpDeleteVerb_WhenRequestParameterIsValidIsbn_ThenShouldReturnStatusNoContent() { // Arrange var isbn = BookStub.CreateNew().Isbn; var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(isbn)).Returns(true); service.Setup(s => s.Delete(isbn)); var controller = new BookController(service.Object); // Act var result = controller.Delete(isbn) as ResponseMessageResult; // Assert service.Verify(s => s.Delete(It.IsAny <string>()), Times.Exactly(1)); result.Should().BeOfType <ResponseMessageResult>(); result.Response.StatusCode.ShouldBeEquivalentTo(204); }
public void GivenHttpPutVerb_WhenRequestParameterIsInvalid_ThenShouldReturnStatusBadRequest() { // Arrange string isbn = null; var book = BookStub.CreateNew(); var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(isbn)).Returns(false); service.Setup(s => s.Update(book)); var controller = new BookController(service.Object); // Act var result = controller.Put(isbn, book) as BadRequestResult; // Assert service.Verify(s => s.Update(It.IsAny <Book>()), Times.Never); result.Should().BeOfType <BadRequestResult>(); }
public void GivenUpdate_WhenInvoked_ThenShouldUpdateBookInRepository() { // Arrange var book = BookStub.CreateNew(); var repository = new Mock <IRepository <Book> >(); repository.Setup(r => r.Retrieve(book.Isbn)) .Returns(book); var service = new BookService(repository.Object); // Act service.Update(book); // Assert repository.Verify(r => r.Update(It.IsAny <Book>()), Times.Exactly(1)); }
public void GivenHttpPostVerb_WhenRequestBodyIsAlreadyExistingBook_ThenShouldReturnStatusConflict() { // Arrange var book = BookStub.CreateNew(); var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(book.Isbn)).Returns(true); service.Setup(s => s.RetrieveByIsbn(book.Isbn)).Returns(book); service.Setup(s => s.Create(book)); var controller = new BookController(service.Object); // Act var result = controller.Post(book) as ConflictResult; // Assert service.Verify(s => s.RetrieveByIsbn(It.IsAny <string>()), Times.Exactly(1)); service.Verify(s => s.Create(It.IsAny <Book>()), Times.Never); result.Should().BeOfType <ConflictResult>(); }
public void GivenHttpPutVerb_WhenBothIsbnsAreValidButDoNotIdentifyAnExistingBook_ThenShouldReturnStatusNotFound() { // Arrange var book = BookStub.CreateNew(); var isbn = book.Isbn; var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(isbn)).Returns(true); service.Setup(s => s.Update(book)); service.Setup(s => s.RetrieveByIsbn(book.Isbn)).Returns(null as Book); var controller = new BookController(service.Object); // Act var result = controller.Put(isbn, book) as NotFoundResult; // Assert service.Verify(s => s.Update(It.IsAny <Book>()), Times.Never); result.Should().BeOfType <NotFoundResult>(); }
public void GivenRetrieveByIsbn_WhenInvoked_ThenShouldReturnMatchingBook() { // Arrange var book = BookStub.CreateNew(); var repository = new Mock <IRepository <Book> >(); repository.Setup(r => r.Retrieve(book.Isbn)) .Returns(book); var service = new BookService(repository.Object); // Act var result = service.RetrieveByIsbn(book.Isbn); // Assert result.ShouldBeEquivalentTo(book); repository.Verify(r => r.Retrieve(book.Isbn), Times.Exactly(1)); }
public void GivenRetrieve_WhenInvokedWithIsbn_ThenShouldReturnAMatchingBook() { // Arrange var book = BookStub.CreateNew(); var set = new Mock <DbSet <Book> >(); set.Setup(s => s.Find(book.Isbn)).Returns(book); var context = new Mock <DbContext>(); context.Setup(c => c.Set <Book>()).Returns(set.Object); var repository = new BookRepository(context.Object); // Act var result = repository.Retrieve(book.Isbn); // Assert set.Verify(s => s.Find(It.IsAny <object[]>()), Times.Exactly(1)); result.ShouldBeEquivalentTo(book); }
public void GivenHttpPutVerb_WhenBothIsbnsAreValidAndIdentifyAnExistingBook_ThenShouldReturnStatusNoContent() { // Arrange var book = BookStub.CreateNew(); var isbn = book.Isbn; var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(isbn)).Returns(true); service.Setup(s => s.Update(book)); service.Setup(s => s.RetrieveByIsbn(book.Isbn)).Returns(book); var controller = new BookController(service.Object); // Act var result = controller.Put(isbn, book) as ResponseMessageResult; // Assert service.Verify(s => s.Update(It.IsAny <Book>()), Times.Exactly(1)); result.Should().BeOfType <ResponseMessageResult>(); result.Response.StatusCode.ShouldBeEquivalentTo(204); }
public void GivenHttpPostVerb_WhenRequestBodyHasValidBook_ThenShouldReturnStatusCreatedAndLocationHeader() { // Arrange var book = BookStub.CreateNew(); var service = new Mock <IBookService>(); service.Setup(s => s.IsValidIsbn(book.Isbn)).Returns(true); service.Setup(s => s.RetrieveByIsbn(book.Isbn)).Returns(null as Book); var controller = new BookController(service.Object); var location = string.Format("/book/{0}", book.Isbn); // Act var result = controller.Post(book) as CreatedNegotiatedContentResult <Book>; // Assert service.Verify(s => s.RetrieveByIsbn(It.IsAny <string>()), Times.Exactly(1)); service.Verify(s => s.Create(It.IsAny <Book>()), Times.Exactly(1)); result.Should().BeOfType <CreatedNegotiatedContentResult <Book> >(); result.Content.ShouldBeEquivalentTo(book); result.Location.ShouldBeEquivalentTo(location); }