Act & Assert Method

After creating the constructor, as we did in our first demo, we need to initialize the ViewResult.

Our ViewResult is come from Index ActionResult in controller. Unlike previous demo that uses ViewBag, we have more complex result to be viewed. To make it easy, we could change the ActionResult into ViewResult.

As we already know, if the index being filled with collection of products, we need to set the Model in our View. The way to do that in our Testing shown below.

// Act
HomeController controller = new HomeController(productRepository);
ViewResult viewResult = controller.Index();
var model = viewResult.Model as IEnumerable<Product>;

Now, we move to Assert method.

Basically, we want to test the controller to throw the products that we mocked earlier. Total is 2 products. We could do something like this.

// Assert
Assert.AreEqual(2, model.Count());

Run the test.

Yeeay, first step, Red, accomplished. We need to add more code to make it Green / pass the test.

results matching ""

    No results matching ""