MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception

This occurred to me when I updated the Castle.Core NuGet Package to the version 4.0.0. Something changed that is does not work properly with latest Moq (v4.5.30) at this moment.

I solved this by going back to the Castle.Core version 3.3.3.


I was able to run your test successfully after making the following changes:

  1. Made TestSetup() public
  2. In RunTest, changed int val = obj.Value to int val = obj.GetValue() - this was just to get the Assert to pass.

I'm not familiar with NUnit (I use xUnit), but my guess is TestSetup() being private was the problem. When that method is private, NUnit shows this exception for me:

Prototype.UnitTests.TestProgram.RunTest:
Invalid signature for SetUp or TearDown method: TestSetup

Maybe you are using an older version of NUnit that handled this situation differently (I just downloaded 2.5.7.10213).

HTH


I had a similar exception with Moq (it had previously worked fine).

For me the solution was to use NuGet to uninstall Moq and the assembly that the exception mentioned. And then re-install Moq using NuGet and apply any NuGet updates that subsequently appeared.

Tags:

Nunit

Mocking

Moq