C#ASP.NET MVC控制器单元测试 [英] C# ASP.NET MVC Controller unit test
本文介绍了C#ASP.NET MVC控制器单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一种新的单元测试,我想知道如果我做的正确与否。
I'm kind of new to unit testing and I am wondering if I'm doing it correct or not.
//Controller
public ActionResult Index()
{
return View("../Message/Index");
}
[TestMethod]
public void MessageViewCorrectTest()
{
var controller = new MessageController();
var result = controller.Index() as ViewResult;
Assert.AreEqual("../Message/Index", result.ViewName);
}
[TestMethod]
public void MessageViewInCorrectTest()
{
var controller = new MessageController();
var result = controller.Index() as ViewResult;
Assert.AreNotEqual("Something/Else", result.ViewName);
}
我这样做是正确的,有没有更好的办法,或者这是好?
Am I doing it right, is there a better way or is this good?
任何反馈将是AP preciated,先谢谢了。
Any feedback would be appreciated, thanks in advance.
推荐答案
下面是一种方法,你可以做到这一点。您也可以验证是否根据您的机型。
Here is a way you can do it. You can also verify based on your model type
[TestMethod]
public void TestMethod2()
{
MessageController controller = new MessageController();
ActionResult result = controller.Index(1);
Assert.IsInstanceOfType(result, typeof(ViewResult));
//Since view has been asserted as ViewResult
ViewResult viewResult = result as ViewResult;
if(viewResult != null)
{
Assert.IsInstanceOfType(viewResult.Model, typeof(YourModelType));
//Further Asserts for your model
}
}
这篇关于C#ASP.NET MVC控制器单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文