控制器的单元测试 [英] Unit testing for controller

查看:62
本文介绍了控制器的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为控制器创建单元测试。在我的控制器中我有以下代码...





使用System;

使用System.Collections。 Generic;

使用System.Linq;

使用System.Web;

使用System.Web.Mvc;

namespace Crossover.SmallApps.Controllers

{

public class SkillsController:Controller

{

//

//获取:/技能/



公共ActionResult索引(int?id)

{

if(id.HasValue)

返回查看(DBHelper.Skills.Select(id.Value));

返回查看();



}

public JsonResult All(int?id)

{



返回Json(DBHelper.Skills.Select());

}



public JsonResult Details(int id)

{

返回Json(DBHelper.Skil ls.Select(id));

}

公共ActionResult更新(Utility.Entity.Skills技能)

{

Utility.Entity.Skills obj = DBHelper.Skills.Select(skills.Id);

obj.Name = skills.Name;

DBHelper.Skills.Update (obj);

返回新的HttpStatusCodeResult(200,OK);



}

public ActionResult添加(Utility.Entity.Skills技能)

{

DBHelper.Skills.Add(new Utility.Entity.Skills

{

Name = skills.Name

});

返回新的HttpStatusCodeResult(200,OK);



}



}

}





我想要的是为上面的所有方法创建单元测试。你的小帮助对我来说将是非常有用的帮助...:)

i want to create unit test for controller. in my controller i have following codes...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Crossover.SmallApps.Controllers
{
public class SkillsController : Controller
{
//
// GET: /Skills/

public ActionResult Index(int? id)
{
if (id.HasValue)
return View(DBHelper.Skills.Select(id.Value));
return View();

}
public JsonResult All(int? id)
{

return Json(DBHelper.Skills.Select());
}

public JsonResult Details(int id)
{
return Json(DBHelper.Skills.Select(id));
}
public ActionResult Update(Utility.Entity.Skills skills)
{
Utility.Entity.Skills obj = DBHelper.Skills.Select(skills.Id);
obj.Name = skills.Name;
DBHelper.Skills.Update(obj);
return new HttpStatusCodeResult(200, "OK");

}
public ActionResult Add(Utility.Entity.Skills skills)
{
DBHelper.Skills.Add(new Utility.Entity.Skills
{
Name=skills.Name
});
return new HttpStatusCodeResult(200, "OK");

}

}
}


what i want is to create unit testing for all methods above.your little help will be great help for me thankyou... :)

推荐答案

http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs [<一个href =http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cstarget =_ blanktitle = 新窗口> ^ ]


这篇关于控制器的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆