Ninject.MockingKernel.Moq 安全异常
我正在使用 Ninject 作为我的 IoC 容器,并且正在尝试编写一些单元测试。我发现了 Ninject Mocking Kernel,所以我想尝试一下,但我无法通过最简单的测试。我缺少一些东西,需要一点帮助。
起订量4.0.10827.Final Ninject-2.2.0.0-release-net-4.0 Ninject.MockingKernel-2.2.0.0-release-net-4.0
我的单元测试...
[TestMethod]
public void Constructor_CanInitialize()
{
var kernel = new MoqMockingKernel();
var mock = kernel.Get<IDataRepository>(); <--Error here
Assert.IsInstanceOfType(mock, typeof(DataRepository));
}
这是错误...
测试方法 TestFixture.Constructor_CanInitialize 抛出异常: System.TypeAccessException:尝试通过安全透明方法“Ninject.MockingKernel.Moq.MoqMockProvider.Create(Ninject.Activation.IContext)”访问安全关键类型“Moq.Mock”失败。
程序集“Ninject.MockingKernel.Moq,Version=2.2.0.0,Culture=neutral,PublicKeyToken=c7192dc5380945e7”标记有AllowPartiallyTrustedCallersAttribute,并使用2级安全透明度模型。默认情况下,2 级透明度会导致AllowPartiallyTrustedCallers 程序集中的所有方法变得安全透明,这可能是导致此异常的原因。
在c:\Projects\Ninject\ninject.mockingkernel\src\Ninject.MockingKernel.Moq\MoqMockProvider.cs中的Ninject.MockingKernel.Moq.MoqMockProvider.Create(IContext context):第64行 在 c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs 中的 Ninject.Activation.Context.Resolve() 处:第 157 行 在 c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs 中的 Ninject.KernelBase.b_7(IContext context) 处:第 375 行 在 System.Linq.Enumerable.<>c_DisplayClass123.
2.MoveNext() 在 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 源) 在c:\ Projects \ Ninject \ ninject \ src \ Ninject \ Planning \ Targets \ Target.cs中的Ninject.Planning.Targets.Target
1.GetValue(类型服务,IContext父级):第179行 在 C:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs 中的 Ninject.Planning.Targets.Target1.ResolveWithin(IContext Parent) 处:第 147 行 在 C:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs 中的 Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) 处:第 97 行 在 C:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs 中的 Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass2.
2.MoveNext() 在 System.Linq.Buffer1..ctor(IEnumerable
1 源) 在 System.Linq.Enumerable.ToArray(IEnumerable1 源) 在 c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs 中的 Ninject.Activation.Providers.StandardProvider.Create(IContext context) 处:第 80 行 在 c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs 中的 Ninject.Activation.Context.Resolve() 处:第 157 行 在 C:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs 中的 Ninject.KernelBase.
3.b__11(TSource x) 在 System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() 在 System.Linq.Enumerable.
1.MoveNext() 在 System.Linq.Enumerable.Single(IEnumerable`1 源) 在 c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs 中的 Ninject.ResolutionExtensions.Get(IResolutionRoot root, IParameter[] 参数):第 37 行 在 TestFixture.cs 中的 TestFixture.Constructor_CanInitialize() 处:第 32 行
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)
2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator
at System.Linq.Enumerable.SingleOrDefault(IEnumerable1 source)
1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs: line 179
at Ninject.Planning.Targets.Target
at Ninject.Planning.Targets.Target1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs: line 147
2.MoveNext()
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.WhereSelectArrayIterator
at System.Linq.Buffer1..ctor(IEnumerable
1 source)
at System.Linq.Enumerable.ToArray(IEnumerable1 source)
3.b__11(TSource x)
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__DisplayClass12
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
1.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经和同样的问题作斗争有一段时间了。
请参阅此帖子以了解我的努力:https://groups.google.com/d /topic/ninject/xInlcd2b1l8/discussion
假设您正在使用 NuGet,您有两个选择:
您可能希望以任何方式保留项目的选项卡,以便在更新版本可用后通过 NuGet 再次使用它。
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:
Any way you may want to keep a tab on the project to use it via NuGet again once an updated version is available.
聚会迟到了,但这很有效:
Late to the party, but this works: