0

I'm trying to use Autofixture to test my Mvc controller with this code:-

 var fixture = new Fixture().Customize(new AutoMoqCustomization());
 fixture.Customize<ViewDataDictionary>(vdd => vdd.Without(x => x.ModelMetadata));
 var sut = fixture.CreateAnonymous<MyController>();

which throws the following exception

Exception has been thrown by the target of an invocation.
The method or operation is not implemented.

After some googling around this seems to be down to using MVc4 and above, and the fix was to change the Customize line to

fixture.Customize<ControllerContext>(vdd => vdd.Without(x => x.DisplayMode));

Hoever this now throws a new exception

A request for an IntPtr was detected. 
This is an unsafe resource that will crash the process if used, so the
request is denied. A common source of IntPtr requests are requests for
delegates such as Func<T> or Action<T>. If this is the case, the expected
workaround is to Customize (Register or Inject) the offending type by
specifying a proper creational strategy.

I now seem to be stuck.

Has anyone else come accross this problem and solved it? Or know of a different way to easily fixturize my controller?

Thanks

Brett
  • 429
  • 1
  • 5
  • 11
  • 1
    You need to customize both the `ViewDataDictionary` _and_ the `ControllerContext`. So basically you need to keep both lines. See these answers for reference: http://stackoverflow.com/a/14989866/26396 and http://stackoverflow.com/a/6592112/26396 – Enrico Campidoglio Jan 20 '16 at 13:52
  • @EnricoCampidoglio As far as I can tell, customizing ought to be enough. – Mark Seemann Jan 23 '16 at 20:38
  • I've just tried, but I can't reproduce this behaviour with AutoFixture 3.39.0 and Microsoft.AspNet.Mvc 5.2.3. How can we reproduce the issue? – Mark Seemann Jan 23 '16 at 20:39
  • Thanks for all comments - Mark thank you. Digging down, it turns out I had a nuGet error so even though nuGet was reporting 3.39 - the actual version of Autofixture being used was 2.0 - ripped out and re-installed package to 3.39 - all is good. Thank you – Brett Jan 25 '16 at 09:08
  • Of course now with the proper version CreateAnonymous needs to be changed to Create – Brett Jan 25 '16 at 09:17

1 Answers1

0

This was down to my .package file telling me I was using Autofixture 3.39.0, but the test project was actually referencing 2.0.0.

Uninstall the old version and remove from solution and re-add solved the problem.

Brett
  • 429
  • 1
  • 5
  • 11