简体   繁体   English

使用Microsoft Fake Framework MVC4单元测试WebMatrix.WebData

[英]Unit testing WebMatrix.WebData with Microsoft Fake Framework MVC4

I can't test any logoff, login, register action from AccountController with the new Microsoft Fake Framework without having this error message: System.Security.VerificationException: Operation could destabilize the runtime. 在没有此错误消息的情况下,我无法使用新的Microsoft Fake Framework测试AccountController的注销,登录和注册操作:System.Security.VerificationException:操作可能会使运行时不稳定。

The unit test is real simple: 单元测试非常简单:

    [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"]);
    }

Also find this: http://social.msdn.microsoft.com/Forums/en-US/vsunittest/thread/f84962ea-a9b2-4e0d-873b-e3cf8cfb37e2 that talk about the same bug but no answer. 也可以找到以下内容: http : //social.msdn.microsoft.com/Forums/en-US/vsunittest/thread/f84962ea-a9b2-4e0d-873b-e3cf8cfb37e2 ,它谈论的是同一错误,但没有答案。

Thanks! 谢谢!

I asked the same question before VS2012 Update 1 was released ( VerificationException when instantiating MVC controller during unit testing ) and I got the response from a guy from Microsoft who said that they are working on it and it should be available in the next update. 在VS2012 Update 1发布之前,我问了同样的问题( 在单元测试中实例化MVC控制器时,出现了VerificationException ),我得到了微软一个家伙的回应,他说他们正在研究它,并且应该在下一个更新中可用。 Well, nothing since then. 好吧,此后再也没有。

However, in order to get the result and to continue testing using Microsoft Fakes Framework, I wrapped the calls to MVC methods like those in the UrlHelper class with my own private methods that return primitive types like string and then Shim the unit test to give me a desired result. 但是,为了获得结果并继续使用Microsoft Fakes Framework进行测试,我用自己的私有方法(如返回string原始类型)包装了对UrlHelper类中的MVC方法的UrlHelper ,然后对单元测试进行了Shim给我理想的结果。 That way I never made a call to the underlying MVC infrastructure and I got the desired result. 这样,我就从未致电过底层的MVC基础架构,并且得到了理想的结果。 Also, you will need to remove System.Web.Mvc.Fakes reference otherwise VerificationException will keep popping up. 另外,您将需要删除System.Web.Mvc.Fakes引用,否则VerificationException将不断弹出。

If you find this tedious then you should switch to a more mature unit testing framework like Moq or Rhino. 如果您觉得这很乏味,那么应该切换到更成熟的单元测试框架,例如Moq或Rhino。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM