public void IndexTest() { Mock<IBooksRepository> mock = new Mock<IBooksRepository>(); // Arrange var controller = new BooksController(mock.Object); // Act ViewResult result = controller.Index() as ViewResult; // Assert Assert.IsNotNull(result); }
public void IndexJSONTest() { Mock<IBooksRepository> mock = new Mock<IBooksRepository>(); // Add three books to repository mock.Setup(m => m.Books).Returns(GetThreeBooks().AsQueryable()); // BooksController's IndexJSON should return all the books var controller = new BooksController(mock.Object); var result = controller.IndexJSON(); // Make sure that it returned two books Assert.IsNotNull(result); dynamic books = result.Data; Assert.AreEqual(books[0].Name, "Bar"); Assert.AreEqual(books[1].Description, "FooBar"); Assert.AreEqual(books[2].Author, "789"); Assert.AreEqual(books.Count, 3); }
public void DetailsTest() { Mock<IBooksRepository> mock = new Mock<IBooksRepository>(); // Add three books to repository mock.Setup(m => m.Books).Returns(GetThreeBooks()); mock.Setup(m => m.GetById(It.IsAny<int>())).Returns((int i) => GetThreeBooks().Where(x => x.Id == i).Single()); // Arrange var controller = new BooksController(mock.Object); // Act ViewResult result = controller.Details(2) as ViewResult; // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Model, typeof(Book)); Book book = result.Model as Book; Assert.AreEqual(book.Name, "BarFoo"); }