小编dex*_*ith的帖子

需要为基于Razor的View编写单元测试的指针

我正在尝试使用MVC 3 Preview 1的新Razor视图引擎,并且真的想用NUnit/Moq编写一个简单的单元测试.我还没有看到任何实际的例子 - 尽管它是Razor真正的销售功能之一.

因此,如果我有一个使用DBConext对象的Controller(首先是EF4 CTP代码),并且视图根据控制器上调用的动作中加载的模型中提供的项目列表呈现下拉列表,我会喜欢能够测试该元素是否填充了其中的项目.

这是我的控制器:

public class WeatherReportController : Controller, IWeatherReportController
{
    private IWeatherDb _weatherDb;

    public WeatherReportController()
    {
        this._weatherDb = new WeatherDb();
    }

    public ActionResult Index()
    {
        WeatherReportIndexModel model = new WeatherReportIndexModel
        {
            Report = new WeatherReport {
                Username = this.HttpContext.User.Identity.Name,
                WeatherType = new WeatherType()
            },
            WeatherTypeList = _weatherDb.GetAllWeatherTypes()
        };
        return View(model);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

public class WeatherReportIndexModel
{
    private IList<WeatherType> _weatherTypeList = new List<WeatherType>();
    public IList<WeatherType> WeatherTypeList { 
        get 
        {
            return _weatherTypeList;
        }
        set 
        { …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc nunit unit-testing razor asp.net-mvc-2

5
推荐指数
1
解决办法
3816
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-2 ×1

nunit ×1

razor ×1

unit-testing ×1