2

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.

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.

Thanks!

Charles
  • 50,943
  • 13
  • 104
  • 142
Karine
  • 385
  • 1
  • 7
  • 18

1 Answers1

1

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. 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. That way I never made a call to the underlying MVC infrastructure and I got the desired result. Also, you will need to remove System.Web.Mvc.Fakes reference otherwise VerificationException will keep popping up.

If you find this tedious then you should switch to a more mature unit testing framework like Moq or Rhino.

Community
  • 1
  • 1
Huske
  • 9,186
  • 2
  • 36
  • 53
  • Update 2, same errors. Microsoft is destabilizing my runtime – felickz Mar 14 '13 at 14:23
  • @felickz I have just tested with Update 2 and it worked fine. I installed Update 2 a few days ago and thought I might give it a shot. There were no VerificationExceptions. – Huske Apr 08 '13 at 17:12
  • crazy i did not have update 2 installed on the machine i was using. Too many dev machines :) – felickz May 22 '13 at 13:45