-2

I am working through Steve Sanderson's book Pro ASP.NET MVC Framework and I having some issues with one unit test which produce error.

In the example below it test the Paginate.

 [TestMethod]
 public void Can_Paginate()
 {

        Mock<IProductRepository> mock = new Mock<IProductRepository>();

        mock.Setup(m => m.Products).Returns(new Product[] {
        new Product{ProductID=1,Name="P1"},
        new Product{ProductID=2,Name="P2"},
        new Product{ProductID=3,Name="P3"},
        new Product{ProductID=4,Name="P4"},
        new Product{ProductID=5,Name="P5"}
        }.AsQueryable());

        ProductController controller = new ProductController(mock.Object);    
        controller.PageSize = 3;

        IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;

        Product[] prodArray = result.ToArray();
        Assert.IsTrue(prodArray.Length == 2);
        Assert.AreEqual(prodArray[0].Name, "P4");
        Assert.AreEqual(prodArray[0].Name, "P5");
}

The message error is:

Test method UnitTestProject3.Peginate.Can_Peginate threw exception: System.NullReferenceException.

Has anyone run into a similar issue or gotten the test to pass?

Liam
  • 27,717
  • 28
  • 128
  • 190
  • 2
    Does it say which line? – ysrb Oct 10 '13 at 14:46
  • 1
    Can you post the code of List method on the controller? – Alexandr Mihalciuc Oct 10 '13 at 14:48
  • 1
    You don't learn programming from typing code in a book. You learn to program by understanding what you are doing and what the compiler tells you. What a `NullReferenceException` is and how you can debug and annihilate those, is explained [here](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net-and-how-do-i-fix-it). – CodeCaster Oct 10 '13 at 14:51
  • @ysrb 18 line,but this line is empty – user2867505 Oct 10 '13 at 14:52

1 Answers1

1

You made a couple of typos and need to update these lines:

ProductListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model;

and

Assert.AreEqual(prodArray[1].Name, "P5");

In the ProductsController, the tutorial had you update the ViewResult to this:

public ViewResult List(string category, int page = 1)

The book then states "We have changed the signature of the List action method, which will prevent some of our existing unit test methods from compiling. To address this, pass null as the first parameter to the List method in those unit tests that work with the controller." - Pg. 203

Andrew
  • 2,013
  • 1
  • 23
  • 38
  • But it shouldn't throw System.NullReferenceException though. PS I didn't downvote. – ysrb Oct 10 '13 at 15:07
  • I downvoted. What tutorial? This won't throw the System.NullReferenceException. This **could** cause the Assert to fail. But not what the OP is experiencing, so this answer is not useful, so if an answer is not useful you downvote it. Thats how this site works. – Liam Oct 10 '13 at 15:11
  • I have the same book he is reading and it's a tutorial. I updated my answer. – Andrew Oct 10 '13 at 15:13
  • Ok, benefit of the doubt but I still don't get it. – Liam Oct 10 '13 at 15:14
  • 1
    He didn't post the controller so it won't make sense, but in the List ViewResult we added an additional parameter "string category" – Andrew Oct 10 '13 at 15:19