如何亲热单元测试在MVC创建方法? [英] How to perfrom Unit Testing on Create method in MVC?
问题描述
如何执行在MVC应用程序中的单元测试?
我创建了控制器位置。
它有一个像LOCATIONNAME,区,市,引脚$性能C $角
现在,我想执行单元测试,检查位置是否保存在数据库或没有。
如何检查它。
我经历吨的视频,每一个地方,他们只是把单元测试
如添加,分割,减数学运算......
我想知道如何执行创建MVC的方法的单元测试
我有code类似下面
[HttpPost]
公众的ActionResult创建(位置定位)
{
如果(ModelState.IsValid)
{
db.Locations.Add(位置);
db.SaveChanges();
返回RedirectToAction(「指数」);
}
}
为了使您的code测试的,你应该控制的抽象依赖。它使用起来非常顺手库图案抽象的数据访问。 <一href=\"http://mytechworld.officeacuity.com/index.php/2011/05/ninject-and-entity-framework-$c$c-first-easy-repository-patterns/\"相对=nofollow>你的资料库注入到控制器:
公共类LocationController:控制器
{
私人ILocationRepository _locationRepository; 公共LocationController(ILocationRepository locationRepository)
{
_locationRepository = locationRepository;
}
}
现在你可以嘲笑你的资料库。这里是起订量框架的 MvcContrib :
//排列
模拟&LT; ILocationRepository&GT;库=新的模拟&LT; ILocationRepository&GT;();
VAR控制器=新LocationController(repository.Object);
位置位置=新的位置(纽约);
//法案
VAR的结果= controller.Create(位置);
//断言
result.AssertActionRedirect()
.ToAction&所述; LocationController&GT;(C =&GT; c.Index());
repository.Verify(R =&GT; r.Add(位置));
repository.Verify(R =&GT; r.Save());
和可以实现code,这将通过这个测试:
[HttpPost]
公众的ActionResult创建(位置定位)
{
如果(ModelState.IsValid)
{
_locationRepository.Add(位置);
_locationRepository.Save();
返回RedirectToAction(「指数」);
}
}
您可以阅读更多的实施库和测试在这里MVC应用:
<一href=\"http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application\"相对=nofollow>在ASP.NET MVC应用程序执行工作模式的存储库和单位。不错的功能也有<一个href=\"http://www.mindscapehq.com/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/\"相对=nofollow>每个请求工作单位。
How to perform a Unit testing on MVC application ?
I have created the Controller Location. It has properties like LocationName,Area,City,PinCode.
Now, I want to perform unit test to check whether Location saves in DB or not. How to check it.
I have go through tons of videos, every where they just put the Unit test of Mathematical operations like adding,Dividing , subtracting....
I would like to know how to perform the Unit testing of Create method of MVC
I have code something like below
[HttpPost]
public ActionResult Create(Location location)
{
if (ModelState.IsValid)
{
db.Locations.Add(location);
db.SaveChanges();
return RedirectToAction("Index");
}
}
In order to make your code testable, you should abstract dependencies of controller. It's very handy to use Repository pattern to abstract data access. Inject your repository into controller:
public class LocationController : Controller
{
private ILocationRepository _locationRepository;
public LocationController(ILocationRepository locationRepository)
{
_locationRepository = locationRepository;
}
}
Now you can mock your repository. Here is sample test with Moq framework and MvcContrib:
// Arrange
Mock<ILocationRepository> repository = new Mock<ILocationRepository>();
var controller = new LocationController(repository.Object);
Location location = new Location("New York);
// Act
var result = controller.Create(location);
// Assert
result.AssertActionRedirect()
.ToAction<LocationController>(c => c.Index());
repository.Verify(r => r.Add(location));
repository.Verify(r => r.Save());
And you can implement code, which will pass this test:
[HttpPost]
public ActionResult Create(Location location)
{
if (ModelState.IsValid)
{
_locationRepository.Add(location);
_locationRepository.Save();
return RedirectToAction("Index");
}
}
You can read more on implementing repositories and testing MVC applications here: Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application. Nice feature also to have Unit of Work per request.
这篇关于如何亲热单元测试在MVC创建方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!