开发者

Difficulty to start up with basic unit test (Sample from my book -- SportsStore)

I'm really new in TDD and, actually, I'm trying to follow the sample from my book (SportsStore -- Pro ASP.NET MVC Framework/Steve Sanderson/APRESS). I'm on pages 103-105.

Although there are more on this, as new to all of this, I'm concerned with the following statements.

ProductsController controller = new ProductsController(repository);
var result = controller.List(2);
     //...

regarding the above statements, when I write this (开发者_如何学Cas in the book),

var products = result.ViewData.Model as IList<Product>;

I get a compiler error "System.Web.MVC.ActionResult" does not contain a definition for ViewData ..." But, when I remove the List() from the statement, then the compiler error disapear.

var result = controller.List(2);//Doesn't work
var result = controller;//It works

Is something wrong there? I checked Apress website for that book, but there is nothing listed as Errata or issue. So I'm really lost.

Thanks for helping


That is because actionresult does not contains a definition for viewdata howerver viewresult does and viewresult is actually an actionresult so you can cast it to (ViewResult) and then get the viewdata


var products = ((ViewResult)result).ViewData.Model as IList<Product>;


you may also be missing System.Web.Mvc library from your test project


Alternatively you can change the code in your Products Controller from:

public *Action*Result List()
{
     return View(productsRepository.Products.ToList());
}

to:

public *View*Result List()
{
     return View(productsRepository.Products.ToList());
}

ViewResult is a subclass of the base class ActionResult.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜