[英]Unit testing WebMatrix.WebData with Microsoft Fake Framework MVC4
在没有此错误消息的情况下,我无法使用新的Microsoft Fake Framework测试AccountController的注销,登录和注册操作:System.Security.VerificationException:操作可能会使运行时不稳定。
单元测试非常简单:
[TestMethod]
public void LogOff()
{
var AccountController = new AccountController();
RedirectToRouteResult RedirectToRouteResult;
//Scope the detours we're creating
using (ShimsContext.Create())
{
ShimWebSecurity.Logout = () => { };
var test = AccountController.LogOff();
RedirectToRouteResult = AccountController.LogOff() as RedirectToRouteResult;
}
Assert.IsNotNull(RedirectToRouteResult);
Assert.AreEqual("Index", RedirectToRouteResult.RouteValues["Action"]);
Assert.AreEqual("Home", RedirectToRouteResult.RouteValues["controller"]);
}
也可以找到以下内容: http : //social.msdn.microsoft.com/Forums/en-US/vsunittest/thread/f84962ea-a9b2-4e0d-873b-e3cf8cfb37e2 ,它谈论的是同一错误,但没有答案。
谢谢!
在VS2012 Update 1发布之前,我问了同样的问题( 在单元测试中实例化MVC控制器时,出现了VerificationException ),我得到了微软一个家伙的回应,他说他们正在研究它,并且应该在下一个更新中可用。 好吧,此后再也没有。
但是,为了获得结果并继续使用Microsoft Fakes Framework进行测试,我用自己的私有方法(如返回string
原始类型)包装了对UrlHelper
类中的MVC方法的UrlHelper
,然后对单元测试进行了Shim给我理想的结果。 这样,我就从未致电过底层的MVC基础架构,并且得到了理想的结果。 另外,您将需要删除System.Web.Mvc.Fakes引用,否则VerificationException将不断弹出。
如果您觉得这很乏味,那么应该切换到更成熟的单元测试框架,例如Moq或Rhino。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.