Ninject.MockingKernel.Moq security exception

天大地大妈咪最大 提交于 2019-12-22 00:34:10

问题


I am using Ninject for my IoC container and I'm trying to write some unit tests. I found the Ninject Mocking Kernel so I thought I'd give it a go but I can't get the simplest test to pass. I am missing something and need a little help.

Moq.4.0.10827.Final Ninject-2.2.0.0-release-net-4.0 Ninject.MockingKernel-2.2.0.0-release-net-4.0

My unit test...

    [TestMethod]
    public void Constructor_CanInitialize()
    {
        var kernel = new MoqMockingKernel();

        var mock = kernel.Get<IDataRepository>(); <--Error here

        Assert.IsInstanceOfType(mock, typeof(DataRepository));
    }

Here is the error...

Test method TestFixture.Constructor_CanInitialize threw exception: System.TypeAccessException: Attempt by security transparent method 'Ninject.MockingKernel.Moq.MoqMockProvider.Create(Ninject.Activation.IContext)' to access security critical type 'Moq.Mock' failed.

Assembly 'Ninject.MockingKernel.Moq, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.

at Ninject.MockingKernel.Moq.MoqMockProvider.Create(IContext context) in c:\Projects\Ninject\ninject.mockingkernel\src\Ninject.MockingKernel.Moq\MoqMockProvider.cs: line 64 at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs: line 157 at Ninject.KernelBase.b_7(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs: line 375 at System.Linq.Enumerable.<>c_DisplayClass123.<CombineSelectors>b__11(TSource x) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.SingleOrDefault(IEnumerable1 source) at Ninject.Planning.Targets.Target1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs: line 179 at Ninject.Planning.Targets.Target1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs: line 147 at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs: line 97 at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass2.<Create>b__1(ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs: line 81 at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray(IEnumerable1 source) at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs: line 80 at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs: line 157 at Ninject.KernelBase.<Resolve>b__7(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs: line 375 at System.Linq.Enumerable.<>c__DisplayClass123.b__11(TSource x) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.<CastIterator>d__b11.MoveNext() at System.Linq.Enumerable.Single(IEnumerable`1 source) at Ninject.ResolutionExtensions.Get(IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs: line 37 at TestFixture.Constructor_CanInitialize() in TestFixture.cs: line 32


回答1:


I have been fighting the same problem for a little while now. See this thread to follow my effort: https://groups.google.com/d/topic/ninject/xInlcd2b1l8/discussion

Assuming that you are using NuGet you have two options:

  • Don't use Ninject.MockingKernel right now or
  • build it from source yourself.

Any way you may want to keep a tab on the project to use it via NuGet again once an updated version is available.




回答2:


Late to the party, but this works:

var mock = kernel.GetMock<IDataRepository>();


来源:https://stackoverflow.com/questions/7494298/ninject-mockingkernel-moq-security-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!