VerificationException when Faking MVC4 and Instantiating Controller in unit test - by Rich Czyzewski

Status : 

  Deferred<br /><br />
		The product team has reviewed this issue and has deferred it for consideration at a later time.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


15
0
Sign in
to vote
ID 740778 Comments
Status Closed Workarounds
Type Bug Repros 7
Opened 5/5/2012 7:57:04 PM
Access Restriction Public

Description

When unit testing a controller class on the default "Internet Application" template in MVC4, if a Fakes assembly is created for MVC4 itself, a System.Security.VerificationException is issued whenever the default AccountController or HomeController are instantiated. 

If MVC4 is not Faked, then the controller instantiation works just fine. I can provide an example project if necessary.
Sign in to post a comment.
Posted by felickz on 3/1/2013 at 11:15 AM
On VS2012 update 2 and still getting this exception when trying to fake System.Web.MVC.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="System.Web.Mvc" Version="4.0.0.0"/>
<!-- user code (see: http://msdn.microsoft.com/en-us/library/hh708916.aspx) -->
<ShimGeneration>
    <Clear />
    <Add TypeName="UrlHelper"/>
</ShimGeneration>
<!-- /user code -->
</Fakes>

Even when i remove the ShimContext from my test i still get this exception.
Posted by Joshua Weber-MSFT on 11/12/2012 at 8:36 AM
We have recently addressed and fixed this bug. Unfortunately we were not able to get the fix in for the Update 1. It should be available in the next update.

There is a rather ugly work around if you are looking to only consume stubs.

1. Disable shim generation.
2. Rename the generated fakes binary assembly.
3. Remove all references to Fakes from your project.
4. Add in a new direct reference to the renamed assembly in step 2.

Without the reference to Fakes generation, the shim detouring engine will not be invoked and will not cause this verification exception allowing consumption and usage of the compile time only stubs.

Thanks,
Joshua Weber

Posted by Sohnee on 10/18/2012 at 4:03 AM
I am having the same problem in System.Web.Http.Controllers using plain StubHttpControllerContext and StubHttpActionDescriptor fakes.

These are required to test custom attributes in ASP.NET WEB API as you need to pass a HttpActionContext. I am having to use Rhino Mocks in the meantime...

            controllerContext = MockRepository.GenerateStub<HttpControllerContext>();
            actionDescriptor = MockRepository.GenerateStub<HttpActionDescriptor>();
            context = new HttpActionContext(_controllerContext, _actionDescriptor);

An update or workaround would be a real help.
Posted by monsteruk on 8/6/2012 at 12:06 PM
Same issue. When we add

ShimUrlHelper.AllInstances.IsLocalUrlString

To our unit test, we get the System.Security.VerificationException. If remove fakes, no exception on controller construction!

:-(
Posted by SteveSmith on 7/7/2012 at 12:02 PM
Getting same behavior when testing an MVC 4 Internet Application and faking WebMatrix.WebData.
Posted by Doug Turnure [Feedback Moderator] on 5/7/2012 at 3:57 PM
Routing to product team.
Posted by MS-Moderator01 on 5/6/2012 at 6:54 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)