6

I try to use the MoqMockingKernel class. (Ninject.MockingKernel.Moq) from the Ninject.MockingKernel Extension for a unit test.

At initializing the MoqMockingKernel I'm getting the following error:

System.TypeLoadException: System.TypeLoadException: Inheritance security rules violated by type: 'Ninject.MockingKernel.MockingKernel'. Derived types must either match the security accessibility of the base type or be less accessible..

My initializing code:

        private MoqMockingKernel mockingKernel;
        private Mock<IUnitOfWork> unitOfWorkMock;
        private IExternalServiceRepository externalServiceRepository;

        [TestInitialize]
        public void Initialize()
        {
            this.mockingKernel = new MoqMockingKernel();
            this.mockingKernel.Bind<IUnitOfWork>().ToMock();

            this.unitOfWorkMock = this.mockingKernel.GetMock<IUnitOfWork>();

            externalServiceRepository = new ExternalServiceRepository { Kernel = this.mockingKernel };
        }

How can I solve this TypeLoadException?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
bitsmuggler
  • 1,689
  • 2
  • 17
  • 30

1 Answers1

5

Are you using .Net framework >= 4.0?

See this conversation

I took advice from those instructions: I modified MockingKernel source code by adding this line

[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]

to AssemblyInfo.cs in projects Ninject.MockingKernel and Ninject.MockingKernel.Moq. Then I recompiled solution, installed dll's and my code started to work!

I hope this is the trick for you.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
vainipet
  • 51
  • 1
  • 2