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

Related searches

I'm getting the exception

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

using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq-NoCastle version. I've tried both this and version in the Moq folder. Both with the same result.

I've got a solution with 2 projects, one for my interface, one for my tests. My main project has 2 files:

IMyInterface.cs:

using System;

namespace Prototype
{
    public interface IMyInterface
    {
        int Value { get; set; }
        void ProcessValue();
        int GetValue();
    }
}

My program.cs file has just the default code that's generated with the project.

My test project has a single file for my dummy test - TestProgram.cs

using System;
using NUnit.Framework;
using Moq;

namespace Prototype.UnitTests
{
    [TestFixture]
    public class TestProgram
    {
        Mock<IMyInterface> mock;

        [TestFixtureSetUp]
        void TestSetup()
        {
            mock = new Mock<IMyInterface>();
            mock.Setup(x => x.GetValue()).Returns(2);
        }

        [Test]
        public void RunTest()
        {
            IMyInterface obj = mock.Object; /* This line fails */
            int val = obj.GetValue();
            Assert.True(val == 2);
        }
    }
}

According to the documentation all is good and proper, and it compiles nicely. The problem comes when I try to run the test. It gets to the line marked above and crashes with the exception:

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

I can't see what's going wrong here, can anyone shed some light on it?

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

MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception, The type initializer for 'Moq.Mock`1' threw an exception. using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq- NoCastle� After upgrading to Moq 4.8 on build server (where we do not have anything netstandard 2.0) started to fail with this: Information: [xUnit.net 00:00:10.0169373] System.TypeInitializationException : The type initializer for 'Moq.DefaultVal

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.

Mocking classes doesn`t work � Issue #773 � moq/moq4 � GitHub, TypeInitializationException The type initializer for 'Moq.ProxyFactory' threw an exception. at qbunia opened this issue on Mar 5, 2019 � 4 comments. Closed� Close Visual Studio and navigate to your solution directory and look for a folder named "packages". Check if there is a folder named Moq. If so, delete it. Reinstall the Moq packages via Package Manager Console or via the "Manage Nuget Packages" option for every test project dependent on Moq library.

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.

I get an error after updating Castle.Core to 4.1.1 � Issue #418 � moq , TypeInitializationException : The type initializer for 'Moq. Consider changing `[ AssemblyVersion]` to major version only (4.0.0.0) to save users� TypeInitializationException was unhandled Message = The type initializer for 'Moq.Mock`1' threw an exception. Source = Moq TypeName = Moq. Mock ` 1 StackTrace: at Moq. Mock ` 1. < InitializeInstance > b__0 in d: \Code\moq\src\Source\Mock. Generic. cs: line 138 at Moq. PexProtector. Invoke (Action action) in d: \Code\moq\src\Source\PexProtector

I also had this issue using Moq, but slightly different than Richard.

My error was the following.

Message: System.TypeInitializationException : The type initializer for 'Moq.Mock`1' threw an exception. ---- System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

In my case I didn't have to remove Mock, install the missing Assembly in the correct version. I don't know why this was only an issue now. The issue happend after merging branches, but both branches didn't have either assemblies nor showed this error before. However, end good all good.

MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an - iOS, MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception - nunit. System.TypeInitializationException: The type initializer for 'Scope' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<Token>' or one of its dependencies. The system cannot find the file specified. System.TypeInitializationException:

Additional information: The type initializer for 'Module' threw an exception. Posted 22-Mar-10 3:06am. Member 3648847. Updated 9-Nov-19 2:10am Hans Dietrich. v2.

At first glance, that assembly version looks suspicious. Yep, confirmed: Castle.Core 4.3.1 has an assembly version of 4.0.0.0 and your project is trying to load 1.0.3.0, so it appears to be misconfigured (e.g. with a bad assembly reference in your .csproj or a bad assembly binding redirect in App.config), or you have a bad Castle.Core assembly lying around somewhere—check your project's obj

As far as I see current version of the System.ValueTuple assembly in the .Net Framework is 4.06.26515.6 but the latest version in the nuget package is 4.0.3.0. It means if developer wants to use the nuget package System.ValueTuple then it's required to forcibly set binding redirect from 0.0.0.0-4.99.99.99 to 4.0.3.0. – Andrey Burykin Jul 8 at

Comments
  • Thanks. It looks like the 'public' accessor needed to be set on the setup. What I couldn't figure out was when I used the debugger to debug the test, it actually ran through that method. So my assumption was that it didn't need to be public.
  • You're welcome. Sounds like NUnit was behaving strangely, and not giving good feedback.
  • Yes Alfa, Agree with you. SetUp : System.TypeInitializationException : The type initializer for 'Moq.Mock`1' threw an exception. ----> System.TypeInitializationException : The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception. ----> System.IO.FileLoadException : Could not load file or assembly 'Castle.Core, Version=4.0.0.0, Culture=neutral,