示例#1
0
        public void ShouldNotCreateExistingAuthor()
        {
            var model = new Author()
            {
                Name       = "Author",
                Biography  = "Biography",
                PictureUrl = "myPicture.jpg"
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(model);
            });

            UsingSession((session) =>
            {
                WaitForTheLastWrite <Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = (System.Web.Mvc.ViewResult)(controller.Create(model));

                Assert.AreEqual("An author with this name already exists", controller.TempData["flashError"]);

                Assert.AreEqual("", viewResult.MasterName);
            });
        }
示例#2
0
        public void ShouldOnlyListAuthorFirst4BooksInAuthorDetailPage()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);
                Enumerable.Range(1, 9)
                .ToList()
                .ForEach(i => repository.Create(new Book()
                {
                    Title = "Book " + i, Author = author1.Name, CreatedAt = DateTime.UtcNow.AddDays(-i)
                }));
            });

            UsingSession((session) =>
            {
                var author          = WaitForTheLastWrite <Author>(session);
                var controller      = new AuthorsController(new Repository(session));
                var viewResult      = controller.Details(author.Id);
                var authorViewModel = (AuthorViewModel)(viewResult.Model);
                Assert.AreEqual(4, authorViewModel.Books.Count);
                Assert.IsTrue(authorViewModel.HasMoreBooks);
            });
        }
示例#3
0
        public void ShouldCreateNewAuthor()
        {
            var model = new Author()
            {
                Name       = "Author",
                Biography  = "Biography",
                PictureUrl = "myPicture.jpg"
            };
            RedirectToRouteResult actionResult = null;

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));

                actionResult = (RedirectToRouteResult)(controller.Create(model));
                Assert.AreEqual("Authors", actionResult.RouteValues["controller"]);
                Assert.AreEqual("Details", actionResult.RouteValues["action"]);
            });

            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite <Author>(session);
                Assert.AreEqual(author.Id, actionResult.RouteValues["id"]);
                AuthorsContollerTestHelper.AssertEqual(model, author);
            });
        }
示例#4
0
        public void ShouldRetrieveTheAuthorToEdit()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
            });

            UsingSession((session) =>
            {
                var author       = WaitForTheLastWrite <Author>(session);
                var controller   = new AuthorsController(new Repository(session));
                var viewResult   = controller.Edit(author.Id);
                var authorInView = (Author)(viewResult.Model);

                AuthorsContollerTestHelper.AssertEqual(author1, authorInView);
            });
        }
示例#5
0
        public void Create_ValidModelState_ReturnsRedirectToIndex()
        {
            // Arrange
            _mockRepo.Setup(repo => repo.GetAll()).Returns(GetTestData());
            _mockUnitOfWork.Setup(uow => uow.Authors).Returns(_mockRepo.Object);
            var author = new AuthorViewModel()
            {
                Name = "Author"
            };

            // Act
            var result = _controller.Create(author);

            // Assert
            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Index", viewResult.ActionName);
        }
示例#6
0
        public void ShouldListAuthors()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            var author2 = new Author()
            {
                Name       = "Author2",
                Biography  = "Biography2",
                PictureUrl = "myPicture2.jpg",
                CreatedAt  = DateTime.UtcNow.AddDays(-1)
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
                controller.Create(author2);
            });

            UsingSession((session) =>
            {
                WaitForTheLastWrite <Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.List();
                var authors    = (IPagedList <Author>)(viewResult.Model);

                Assert.AreEqual("", viewResult.MasterName);
                Assert.AreEqual("Authors", viewResult.ViewBag.Title);

                Assert.AreEqual(2, authors.Count);
                AuthorsContollerTestHelper.AssertEqual(author1, authors.ElementAt(0));
                AuthorsContollerTestHelper.AssertEqual(author2, authors.ElementAt(1));
            });
        }
示例#7
0
        public void ShouldNotEditAuthorNameToAnExsitingOne()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            var author2 = new Author()
            {
                Name       = "Author2",
                Biography  = "Biography2",
                PictureUrl = "myPicture2.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
                controller.Create(author2);
            });

            UsingSession((session) =>
            {
                WaitForTheLastWrite <Author>(session);

                var author  = session.Query <Author>().First(a => a.Name == author2.Name);
                author.Name = author1.Name;

                var controller = new AuthorsController(new Repository(session));
                var viewResult = (System.Web.Mvc.ViewResult)(controller.Edit(author));

                Assert.AreEqual("An author with this name already exists", controller.TempData["flashError"]);

                Assert.AreEqual("", viewResult.MasterName);
            });
        }
示例#8
0
        public void DetailsShouldAuthorsBooks()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            var book1 = new Book
            {
                Title     = "Oliver Orphan",
                Author    = author1.Name,
                AgeRange  = "0~2",
                CreatedAt = DateTime.UtcNow
            };

            var book2 = new Book
            {
                Title     = "Oliver Orphan2",
                Author    = author1.Name,
                AgeRange  = "0~2",
                CreatedAt = DateTime.UtcNow.AddDays(-1)
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);

                repository.Create(book1);
                repository.Create(book2);
            });

            using (var session = _documentStore.OpenSession())
            {
                var author          = WaitForTheLastWrite <Author>(session);
                var controller      = new AuthorsController(new Repository(session));
                var result          = (ViewResult)controller.Details(author.Id);
                var authorViewModel = (AuthorViewModel)result.Model;

                AuthorsContollerTestHelper.AssertEqual(authorViewModel.Author, author1);
                var books = authorViewModel.Books;
                Assert.AreEqual(2, books.Count());
                Assert.IsFalse(authorViewModel.HasMoreBooks);

                Assert.AreEqual(book1.Title, books.First().Title);
                Assert.AreEqual(book2.Title, books.Last().Title);
            }
        }
示例#9
0
        public void ShouldEditAuthor()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                controller.Create(author1);
            });

            Author updatedAuthorInfo = null;

            UsingSession((session) =>
            {
                var author = WaitForTheLastWrite <Author>(session);

                updatedAuthorInfo = new Author()
                {
                    Id         = author.Id,
                    Name       = "Author updated",
                    Biography  = "Biography updated",
                    PictureUrl = "myPictureupdated.jpg",
                };
                var controller   = new AuthorsController(new Repository(session));
                var actionResult = (RedirectToRouteResult)(controller.Edit(updatedAuthorInfo));
                Assert.AreEqual("Authors", actionResult.RouteValues["controller"]);
                Assert.AreEqual("Details", actionResult.RouteValues["action"]);
                Assert.AreEqual(author.Id, actionResult.RouteValues["id"]);
            });

            UsingSession((session) =>
            {
                var updatedAuthor = WaitForTheLastWrite <Author>(session);
                AuthorsContollerTestHelper.AssertEqual(updatedAuthorInfo, updatedAuthor);
            });
        }
示例#10
0
        public void AddAuthorToDatabase()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAuthorDatabase")
                          .Options;

            var context    = new ApplicationDbContext(options);
            var controller = new AuthorsController(context);
            var result     = controller.Create(new Author {
                Firstname = "test", Lastname = "testing", BirthYear = 2000
            }).Result;


            Assert.IsType <RedirectToActionResult>(result);
            using (var ctx = new ApplicationDbContext(options))
            {
                Assert.Equal(1, ctx.Author.Count());
                Assert.Equal("test", ctx.Author.Single().Firstname);
            }
        }
示例#11
0
        public void ShouldListAuthorBooksWithPagination()
        {
            var author1 = new Author()
            {
                Name       = "Author1",
                Biography  = "Biography1",
                PictureUrl = "myPicture1.jpg",
                CreatedAt  = DateTime.UtcNow
            };

            UsingSession((session) =>
            {
                var repository = new Repository(session);
                var controller = new AuthorsController(repository);
                controller.Create(author1);
                Enumerable.Range(1, 22)
                .ToList()
                .ForEach(i => repository.Create(new Book()
                {
                    Title = "Book " + i, Author = author1.Name, CreatedAt = DateTime.UtcNow.AddDays(-i)
                }));
            });

            UsingSession((session) =>
            {
                var author     = WaitForTheLastWrite <Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.Books(author.Id);
                var books      = (IPagedList <Book>)(viewResult.Model);
                Assert.AreEqual(9, books.Count);
                Assert.AreEqual(3, books.PageCount);

                viewResult = controller.Books(author.Id, 3, 9);
                books      = (IPagedList <Book>)(viewResult.Model);
                Assert.AreEqual(4, books.Count);
                Assert.AreEqual(3, books.PageCount);

                Assert.AreEqual(author.Name, ((Author)(viewResult.ViewBag.Author)).Name);
            });
        }
示例#12
0
        public void ShouldListAuthorsWithPagination()
        {
            UsingSession((session) =>
            {
                var controller = new AuthorsController(new Repository(session));
                Enumerable.Range(1, 9)
                .ToList()
                .ForEach(i => controller.Create(new Author {
                    Name = "Author " + i, Biography = "Biography " + i
                }));
            });

            UsingSession((session) =>
            {
                WaitForTheLastWrite <Author>(session);
                var controller = new AuthorsController(new Repository(session));
                var viewResult = controller.List(1, 4);
                var authors    = (IPagedList <Author>)(viewResult.Model);
                Assert.AreEqual(4, authors.Count);
                Assert.AreEqual(3, authors.PageCount);
            });
        }