Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MoqMockingKernel: System.TypeLoadException: Inheritance security rules violated by type

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?

like image 290
bitsmuggler Avatar asked Nov 08 '12 10:11

bitsmuggler


1 Answers

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.

like image 148
vainipet Avatar answered Nov 08 '22 00:11

vainipet