public void TestGet(int id) { var result = _controller.Get(id) as OkObjectResult; var book = result.Value as BookFast; Assert.AreEqual(book.Id, id); }
public async void GetBooks_ShoudBeCorrectAsync() { var result = await _bookService.Get(); Assert.IsNotNull(result); Assert.Positive(((result as ViewResult)?.Model as IList <Book>)?.Count ?? 0); }
public void GetByCode() { var cancellationToken = new CancellationToken(false); var result = _booksController.Get("NET-0001").ExecuteAsync(cancellationToken).Result; Assert.IsTrue(result.StatusCode == HttpStatusCode.OK); }
public void Get_WhenCalled_ReturnsOkResult() { // Act var okResult = _controller.Get(); // Assert Assert.IsType(GetTestBooks().GetType(), okResult); }
public void Book_Found_By_Id() { InsertSingleBook(); var response = booksController.Get(1); Assert.AreNotEqual("null", response); }
public void GetBooksOkResultTest() { var response = _controller.Get().Result as OkObjectResult; Assert.IsNotNull(response); Assert.AreEqual(200, response.StatusCode); Assert.IsInstanceOfType(response.Value, typeof(IQueryable <BookDto>)); Assert.AreEqual(9, ((IQueryable <BookDto>)response.Value).ToList().Count); }
public async Task GetBooksAsyncAllTitlesNotNullOrEmpty() { var actResults = await _booksController.Get() as JsonResult; var booksList = (List <Book>)actResults.Value; Assert.That(booksList.Select(x => x.Title), Is.All.Not.Null); Assert.That(booksList.Select(x => x.Title), Is.All.Not.Empty); }
public async Task ItShallReturnBooksContainingTitle() { // Given A.CallTo(() => nameGenerator.GenerateBookTitleAsync()).Returns(new ValueTask <string>("expected-book-title")); // When var books = await subjectUnderTest.Get(); // Then books.Should().HaveCount(1); books.All(b => b.Title == "expected-book-title").Should().BeTrue(); }
public void BooksControllerGetPOS1() { //Act var result = _bController.Get(); var books = result.Value; //Assert Assert.IsType <List <Book> >(books); //Check if method gets all the books from the store Assert.Matches(books.Count.ToString(), "9"); //Test if all books have id in GUID format }
public void GetAllTest() { // arrange (skip because we have no parameters) // act var result = _controller.Get(); // assert Assert.IsType <OkObjectResult>(result.Result); var list = result.Result as OkObjectResult; Assert.IsType <List <Book> >(list.Value); var listBooks = list.Value as List <Book>; Assert.Equal(5, listBooks.Count); }
public async Task Get_SingleBookById() { // Arrange var bookRepo = new Mock <IBookRepository>(); var bookHistoryRepo = new Mock <IBookHistoryRepository>(); //var bookFeedbackRepo = new Mock<IBookFeedbackRepository>(); var authRepo = new Mock <IAuthenticationRepository>(); var personRepository = new Mock <IPersonRepository>(); var bookOrderRepository = new Mock <IBookOrderRepository>(); bookRepo.Setup(x => x.GetById(It.IsAny <int>())) .Returns(async(int id) => { return(GetStaticBooks().Where(x => x.Id == id).Single().ToDetailedDto()); }); var controller = new BooksController(bookRepo.Object, bookHistoryRepo.Object, authRepo.Object, personRepository.Object, bookOrderRepository.Object); // Act IHttpActionResult actionResult = await controller.Get(1); var contentResult = actionResult as OkNegotiatedContentResult <DetailedBookDto>; // Assert DetailedBookDto expectedBookDto = GetStaticBooks().First().ToDetailedDto(); Assert.IsNotNull(contentResult); Assert.IsNotNull(contentResult.Content); Assert.AreEqual(expectedBookDto.Id, contentResult.Content.Id); Assert.AreEqual(JsonConvert.SerializeObject(expectedBookDto), JsonConvert.SerializeObject(contentResult.Content)); }
public async Task Get_Returns_All_Books() { // Arrange var stubRepository = new Mock <IBookRepository>(); var stubLogger = new Mock <ILogger <BooksController> >(); stubRepository .Setup(x => x.GetAllBooksAsync()) .ReturnsAsync(new[] { new Book { Id = "1" }, new Book { Id = "2" }, new Book { Id = "3" }, new Book { Id = "4" }, new Book { Id = "5" } }.AsEnumerable()); var controller = new BooksController(stubRepository.Object, stubLogger.Object); // Act var contacts = await controller.Get(); // Assert Assert.Equal(5, contacts.Count()); }
public async Task Get_SingleBookReturnsNullOnNotFound() { // Arrange var bookRepo = new Mock <IBookRepository>(); var bookHistoryRepo = new Mock <IBookHistoryRepository>(); var bookFeedbackRepo = new Mock <IBookFeedbackRepository>(); var authRepo = new Mock <IAuthenticationRepository>(); var personRepository = new Mock <IPersonRepository>(); var bookOrderRepository = new Mock <IBookOrderRepository>(); bookRepo.Setup(x => x.GetById(100)) .Returns(async() => { return(GetStaticBooks().Where(x => x.Id == 100).SingleOrDefault()?.ToDetailedDto()); }); var controller = new BooksController(bookRepo.Object, bookHistoryRepo.Object, authRepo.Object, personRepository.Object, bookOrderRepository.Object); // Act IHttpActionResult actionResult = await controller.Get(100); var contentResult = actionResult as OkNegotiatedContentResult <DetailedBookDto>; // Assert Assert.IsNull(contentResult.Content); }
public async Task Success() { Mock <IBooksService> serviceMock = new Mock <IBooksService>(); serviceMock.Setup(x => x.GetByParamsAsync( It.IsAny <bool?>(), It.IsAny <int[]>(), It.IsAny <decimal?>(), It.IsAny <decimal?>(), It.IsAny <string>(), It.IsAny <string?>(), It.IsAny <CancellationToken>())).ReturnsAsync(new Book[] { new Book(), new Book(), }); var loggerMock = new Mock <ILogger <BooksController> >(); BooksController controller = new BooksController(serviceMock.Object, loggerMock.Object); var filterModel = new FilterModel { Author = "x", Name = "y", Exists = null, MaxCost = null, MinCost = null, Types = null }; var getResult = (await controller.Get(filterModel, CancellationToken.None)) as OkObjectResult; Assert.IsNotNull(getResult); var booksArray = getResult.Value as BookModel[]; Assert.IsNotNull(booksArray); Assert.AreEqual(2, booksArray.Length); }
public async Task ServerError() { Mock <IBooksService> serviceMock = new Mock <IBooksService>(); serviceMock.Setup(x => x.GetByParamsAsync( It.IsAny <bool?>(), It.IsAny <int[]>(), It.IsAny <decimal?>(), It.IsAny <decimal?>(), It.IsAny <string>(), It.IsAny <string?>(), It.IsAny <CancellationToken>())).ThrowsAsync(new Exception()); var loggerMock = new Mock <ILogger <BooksController> >(); BooksController controller = new BooksController(serviceMock.Object, loggerMock.Object); var filterModel = new FilterModel { Author = "x", Name = "y", Exists = null, MaxCost = null, MinCost = null, Types = null }; var getResult = (await controller.Get(filterModel, CancellationToken.None)) as OkObjectResult; Assert.IsNotNull(getResult); var errorResult = getResult.Value as ProblemDetails; Assert.IsNotNull(errorResult); }
public async Task ShouldReturnNotFoundIfBookNotExists() { //arrange var authorId = fixture.Create <int>(); var bookId = fixture.Create <int>(); var bookToReturn = new BookDTO() { Id = bookId, AuthorId = authorId, Title = fixture.Create <string>() }; mockGetBookStrategy .Setup(x => x.GetBookById(authorId, bookId)) .Returns(Task.FromResult <BookDTO>(null)); var sut = new BooksController( mockGetBooksStrategy.Object, mockGetBookStrategy.Object, mockCreateBookStrategy.Object, mockUpdateBookStrategy.Object, mockDeleteBookStrategy.Object, mockBookValidationStrategy.Object); //act var result = await sut.Get(authorId, bookId); //assert Assert.IsInstanceOf <NotFoundResult>(result); }
public async Task ShouldGetBookById() { //arrange var authorId = fixture.Create <int>(); var bookId = fixture.Create <int>(); var bookToReturn = new BookDTO() { Id = bookId, AuthorId = authorId, Title = fixture.Create <string>() }; mockGetBookStrategy .Setup(x => x.GetBookById(authorId, bookId)) .Returns(Task.FromResult(bookToReturn)); var sut = new BooksController( mockGetBooksStrategy.Object, mockGetBookStrategy.Object, mockCreateBookStrategy.Object, mockUpdateBookStrategy.Object, mockDeleteBookStrategy.Object, mockBookValidationStrategy.Object); //act var result = await sut.Get(authorId, bookId); var okResult = result as OkObjectResult; //assert Assert.IsInstanceOf <OkObjectResult>(result); Assert.IsInstanceOf <BookDTO>(okResult.Value); }
public void Get_ShouldReturnOkAndDataFromService() { // Arrange const string expectedIsbn = "9780132350884"; const string expectedAuthor = "Robert C. Martin"; const string expectedTitle = "Clean Code: A Handbook of Agile Software Craftsmanship"; const int expectedPageCount = 464; var data = new List <Book> { new Book { ISBN = expectedIsbn, Author = expectedAuthor, Title = expectedTitle, PageCount = expectedPageCount } }; _booksService.Get().Returns(data); // Act var result = _booksController.Get().Result as OkObjectResult; var resultData = result.Value as IEnumerable <Contract.Book>; // Assert Assert.AreEqual((int)HttpStatusCode.OK, result.StatusCode); Assert.IsTrue(resultData.Any(b => b.ISBN.Equals(expectedIsbn) && b.Author.Equals(expectedAuthor) && b.Title.Equals(expectedTitle) && b.PageCount == expectedPageCount)); }
public async Task GivenThatBookWithGivenIdExists_WhenTryingToGetBook_ThenResultContainsBook() { var service = MockRepository.GenerateMock <IModelService <IBook, BookFilter> >(); var book = MockRepository.GenerateMock <IBook>(); book.Stub(x => x.Name).Return("book"); book.Stub(x => x.Identifier).Return(1); book.Stub(x => x.Country).Return("country"); book.Stub(x => x.ISBN).Return("isbn"); book.Stub(x => x.Authors).Return(new List <string> { "author" }); book.Stub(x => x.Released).Return(new DateTime(2000, 1, 1)); book.Stub(x => x.Publisher).Return("publisher"); book.Stub(x => x.MediaType).Return(MediaType.Hardcover); book.Stub(x => x.NumberOfPages).Return(10); service.Stub(x => x.GetAsync(Arg <int> .Is.Equal(1))).Return(Task.FromResult(book)); var mapper = MockRepository.GenerateMock <IModelMapper <IBook, Book> >(); mapper.Stub(x => x.Map(Arg <IBook> .Matches(b => b.Identifier == 1), Arg <UrlHelper> .Is.Anything)) .Return(new Book("someKindOfUrl/1", book.Name, book.ISBN, book.Authors, book.NumberOfPages, book.Publisher, book.Country, AnApiOfIceAndFire.Models.v1.MediaType.Hardcover, book.Released, new List <string>(), new List <string>())); BaseController <IBook, Book, BookFilter> controller = new BooksController(service, mapper, MockRepository.GenerateMock <IPagingLinksFactory <BookFilter> >()); var result = await controller.Get(1); var okResult = result as OkNegotiatedContentResult <Book>; Assert.IsNotNull(okResult); Assert.AreEqual(book.Name, okResult.Content.Name); Assert.AreEqual("someKindOfUrl/1", okResult.Content.URL); }
public async Task GivenThatNoBooksExists_WhenTryingTogetAll_ThenResultContainsNoBooks() { var service = MockRepository.GenerateMock <IModelService <IBook, BookFilter> >(); var bookPagedList = MockRepository.GenerateMock <IPagedList <IBook> >(); service.Stub(x => x.GetPaginatedAsync(0, 0, null)).IgnoreArguments() .Return(Task.FromResult((IPagedList <IBook>) new PagedList <IBook>(Enumerable.Empty <IBook>().AsQueryable(), 1, 1))); var mapper = MockRepository.GenerateMock <IModelMapper <IBook, Book> >(); mapper.Stub(x => x.Map(null, null)).IgnoreArguments().Return(null); var linksFactory = MockRepository.GenerateMock <IPagingLinksFactory <BookFilter> >(); linksFactory.Stub(x => x.Create(bookPagedList, null, new BookFilter())).IgnoreArguments().Return(new[] { new Link(new Uri("https://localhost.com")) }); var controller = new BooksController(service, mapper, linksFactory) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://localhost.com/something")), }; IEnumerable <Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual(0, books.Count()); }
public async Task GivenThatOneBookExists_WhenTryingToGetAll_ThenResultContainsCorrectBook() { var book = CreateMockedBook(1); var service = MockRepository.GenerateMock <IModelService <IBook> >(); service.Stub(x => x.GetPaginatedAsync(Arg <int> .Is.Anything, Arg <int> .Is.Anything)) .Return(Task.FromResult((IPagedList <IBook>) new PagedList <IBook>(new List <IBook> { book }.AsQueryable(), 1, 1))); var urlHelper = CreateUrlHelper("http://localhost/api/books/1"); var mapper = new BookMapper(new MediaTypeMapper()); var controller = new BooksController(service, mapper) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/api/books/1")), Url = urlHelper }; IEnumerable <Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(1, books.Count()); Assert.AreEqual("http://localhost/api/books/1", books.ElementAt(0).URL); }
public async Task GivenThatOneBookExists_WhenTryingToGetAll_ThenResultContainsCorrectBook() { var book = CreateMockedBook(1); var service = MockRepository.GenerateMock <IModelService <IBook, BookFilter> >(); var pagedList = new PagedList <IBook>(new List <IBook> { book }.AsQueryable(), 1, 1); service.Stub(x => x.GetPaginatedAsync(0, 0)).IgnoreArguments() .Return(Task.FromResult((IPagedList <IBook>)pagedList)); var linksFactory = MockRepository.GenerateMock <IPagingLinksFactory <BookFilter> >(); linksFactory.Stub(x => x.Create(pagedList, null, new BookFilter())).IgnoreArguments().Return(new[] { new Link(new Uri("https://localhost.com")) }); var mapper = new BookMapper(new MediaTypeMapper()); var controller = new BooksController(service, mapper, linksFactory) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost.com")), Url = CreateUrlHelper("http://localhost.com") }; IEnumerable <Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(1, books.Count()); }
public async Task GivenThatBookWithGivenIdExists_WhenTryingToGetBook_ThenResultContainsBook() { var service = MockRepository.GenerateMock<IModelService<IBook, BookFilter>>(); var book = MockRepository.GenerateMock<IBook>(); book.Stub(x => x.Name).Return("book"); book.Stub(x => x.Identifier).Return(1); book.Stub(x => x.Country).Return("country"); book.Stub(x => x.ISBN).Return("isbn"); book.Stub(x => x.Authors).Return(new List<string> { "author" }); book.Stub(x => x.Released).Return(new DateTime(2000, 1, 1)); book.Stub(x => x.Publisher).Return("publisher"); book.Stub(x => x.MediaType).Return(MediaType.Hardcover); book.Stub(x => x.NumberOfPages).Return(10); service.Stub(x => x.GetAsync(Arg<int>.Is.Equal(1))).Return(Task.FromResult(book)); var mapper = MockRepository.GenerateMock<IModelMapper<IBook, Book>>(); mapper.Stub(x => x.Map(Arg<IBook>.Matches(b => b.Identifier == 1), Arg<UrlHelper>.Is.Anything)) .Return(new Book("someKindOfUrl/1", book.Name, book.ISBN, book.Authors, book.NumberOfPages, book.Publisher, book.Country, AnApiOfIceAndFire.Models.v1.MediaType.Hardcover, book.Released, new List<string>(), new List<string>())); BooksController controller = new BooksController(service, mapper, MockRepository.GenerateMock<IPagingLinksFactory<BookFilter>>()); var result = await controller.Get(1); var okResult = result as OkNegotiatedContentResult<Book>; Assert.IsNotNull(okResult); Assert.AreEqual(book.Name, okResult.Content.Name); Assert.AreEqual("someKindOfUrl/1", okResult.Content.URL); }
public async Task GivenThatNoBooksExists_WhenTryingTogetAll_ThenResultContainsNoBooks() { var service = MockRepository.GenerateMock <IModelService <IBook> >(); service.Stub(x => x.GetPaginatedAsync(Arg <int> .Is.Anything, Arg <int> .Is.Anything)) .Return(Task.FromResult((IPagedList <IBook>) new PagedList <IBook>(Enumerable.Empty <IBook>().AsQueryable(), 1, 1))); var mapper = MockRepository.GenerateMock <IModelMapper <IBook, Book> >(); var urlHelper = MockRepository.GenerateMock <UrlHelper>(); urlHelper.Stub(x => x.Link(Arg <string> .Is.Anything, Arg <object> .Is.Anything)).Return("https://localhost.com"); var controller = new BooksController(service, mapper) { Configuration = new HttpConfiguration(), Url = urlHelper, Request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://localhost.com/something")) }; IEnumerable <Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual(0, books.Count()); }
public void BooksControllerGetDetailsOfNonExistantIdShould_ReturnNotFoundStatusCode() { BooksController booksController = GetController(); int nonExistantId = 99; Assert.IsType <NotFoundResult>(booksController.Get(nonExistantId)); }
public async Task Get_should_return_OK_result() { // Arrange var book = new BookModel { Id = Guid.NewGuid() }; A.CallTo(() => _mediator.Send(A <SingleBookQuery> .Ignored, A <CancellationToken> .Ignored)).Returns(book); // Act var result = await _controller.Get(book.Id); // Assert Assert.IsType <OkObjectResult>(result); A.CallTo(() => _mediator.Send(A <SingleBookQuery> .Ignored, A <CancellationToken> .Ignored)).MustHaveHappenedOnceExactly(); }
public void TestGetBookById() { string expectedTitle = "Sample2"; var controller = new BooksController(_booksContext); Book result = controller.Get(2); Assert.Equal(expectedTitle, result.Title); }
public void Books_Controller_Return_one_Book_from_author_exist() { var controller = new BooksController(); IHttpActionResult actionResult = controller.Get(new Guid("25320c5e-f58a-4b1f-b63a-8ee07a840bdf"), new Guid("c7ba6add-09c4-45f8-8dd0-eaca221e5d93")); var contentResult = actionResult as OkNegotiatedContentResult <BookDto>; Assert.IsNotNull(actionResult); Assert.AreEqual("The Shining", contentResult.Content.Title); }
public void Books_Controller_Return_Books_from_author_exist() { var controller = new BooksController(); IHttpActionResult actionResult = controller.Get(new Guid("25320c5e-f58a-4b1f-b63a-8ee07a840bdf")); var contentResult = actionResult as OkNegotiatedContentResult <IEnumerable <BookDto> >; Assert.IsNotNull(actionResult); Assert.IsNotNull(contentResult); }
public void Books_Controller_Return_One_Book_Get_Author_dont_Exist() { var controller = new BooksController(); IHttpActionResult actionResult = controller.Get(new Guid(), new Guid("c7ba6add-09c4-45f8-8dd0-eaca221e5d93")); var contentResult = actionResult as OkNegotiatedContentResult <BookDto>; var notFoundResult = actionResult as NotFoundResult; Assert.IsNull(contentResult); Assert.IsNotNull(notFoundResult); }
public async Task GivenThatBookWithGivenIdDoesNotExist_WhenTryingToGetBook_ThenResultIsOfTypeNotFound() { var service = MockRepository.GenerateMock<IModelService<IBook, BookFilter>>(); service.Stub(x => x.GetAsync(Arg<int>.Is.Anything)).Return(Task.FromResult((IBook)null)); BooksController controller = new BooksController(service, MockRepository.GenerateMock<IModelMapper<IBook, Book>>(), MockRepository.GenerateMock<IPagingLinksFactory<BookFilter>>()); var result = await controller.Get(1); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public void Books_Controller_Return_One_Book_Get_Book_dont_Exist() { var controller = new BooksController(); IHttpActionResult actionResult = controller.Get(new Guid("25320c5e-f58a-4b1f-b63a-8ee07a840bdf"), new Guid()); var contentResult = actionResult as OkNegotiatedContentResult <BookDto>; var notFoundResult = actionResult as NotFoundResult; Assert.IsNull(contentResult); Assert.IsNotNull(notFoundResult); }
public async Task GivenThatOneBookExists_WhenTryingToGetAll_ThenResultContainsCorrectBook() { var book = CreateMockedBook(1); var service = MockRepository.GenerateMock<IModelService<IBook, BookFilter>>(); var pagedList = new PagedList<IBook>(new List<IBook> { book }.AsQueryable(), 1, 1); service.Stub(x => x.GetPaginatedAsync(0, 0)).IgnoreArguments() .Return(Task.FromResult((IPagedList<IBook>)pagedList)); var linksFactory = MockRepository.GenerateMock<IPagingLinksFactory<BookFilter>>(); linksFactory.Stub(x => x.Create(pagedList, null, new BookFilter())).IgnoreArguments().Return(new[] { new Link(new Uri("https://localhost.com")) }); var mapper = new BookMapper(new MediaTypeMapper()); var controller = new BooksController(service, mapper, linksFactory) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost.com")), Url = Helper.CreateUrlHelper("http://localhost.com") }; IEnumerable<Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(1, books.Count()); }
public async Task GivenThatNoBooksExists_WhenTryingTogetAll_ThenResultContainsNoBooks() { var service = MockRepository.GenerateMock<IModelService<IBook, BookFilter>>(); var bookPagedList = MockRepository.GenerateMock<IPagedList<IBook>>(); service.Stub(x => x.GetPaginatedAsync(0, 0, null)).IgnoreArguments() .Return(Task.FromResult((IPagedList<IBook>)new PagedList<IBook>(Enumerable.Empty<IBook>().AsQueryable(), 1, 1))); var mapper = MockRepository.GenerateMock<IModelMapper<IBook, Book>>(); mapper.Stub(x => x.Map(null, null)).IgnoreArguments().Return(null); var linksFactory = MockRepository.GenerateMock<IPagingLinksFactory<BookFilter>>(); linksFactory.Stub(x => x.Create(bookPagedList, null, new BookFilter())).IgnoreArguments().Return(new[] { new Link(new Uri("https://localhost.com")) }); var controller = new BooksController(service, mapper, linksFactory) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://localhost.com/something")), }; IEnumerable<Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual(0, books.Count()); }
public async Task GivenThatNoBooksExists_WhenTryingTogetAll_ThenResultContainsNoBooks() { var service = MockRepository.GenerateMock<IModelService<IBook>>(); service.Stub(x => x.GetPaginatedAsync(Arg<int>.Is.Anything, Arg<int>.Is.Anything)) .Return(Task.FromResult((IPagedList<IBook>)new PagedList<IBook>(Enumerable.Empty<IBook>().AsQueryable(), 1, 1))); var mapper = MockRepository.GenerateMock<IModelMapper<IBook, Book>>(); var urlHelper = MockRepository.GenerateMock<UrlHelper>(); urlHelper.Stub(x => x.Link(Arg<string>.Is.Anything, Arg<object>.Is.Anything)).Return("https://localhost.com"); var controller = new BooksController(service, mapper) { Configuration = new HttpConfiguration(), Url = urlHelper, Request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://localhost.com/something")) }; IEnumerable<Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual(0, books.Count()); }
public async Task GivenThatOneBookExists_WhenTryingToGetAll_ThenResultContainsCorrectBook() { var book = CreateMockedBook(1); var service = MockRepository.GenerateMock<IModelService<IBook>>(); service.Stub(x => x.GetPaginatedAsync(Arg<int>.Is.Anything, Arg<int>.Is.Anything)) .Return(Task.FromResult((IPagedList<IBook>)new PagedList<IBook>(new List<IBook> { book }.AsQueryable(), 1, 1))); var urlHelper = CreateUrlHelper("http://localhost/api/books/1"); var mapper = new BookMapper(new MediaTypeMapper()); var controller = new BooksController(service, mapper) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/api/books/1")), Url = urlHelper }; IEnumerable<Book> books; var result = await controller.Get(); result.TryGetContentValue(out books); Assert.AreEqual(1, books.Count()); Assert.AreEqual("http://localhost/api/books/1", books.ElementAt(0).URL); }