【问题标题】:Ninject: System.Security.VerificationException : Operation could destabilize the runtimeNinject:System.Security.VerificationException:操作可能会破坏运行时
【发布时间】:2012-03-10 14:52:35
【问题描述】:

在执行测试时,我的以下代码运行良好。

然后我尝试运行这些测试 + 代码覆盖率计算 (SharpDevelop 4) 它抛出异常。

有人能描述一下为什么会这样吗?

SetUp : System.Security.VerificationException : 操作可以 破坏运行时的稳定性。

  [TestFixture]
  public class NinjectExamplesTest
  {
    private interface IExampleInterface
    {

    }

    private class ExampleInterfaceImplementation : IExampleInterface
    {

    }

    private class ExampleClass
    {
      [Inject]
      public IExampleInterface ExampleProperty { get; set; }
    }      

    IKernel kernel;   

    [SetUp]
    public void Init()
    {
      kernel = new StandardKernel();
      kernel.Bind<IExampleInterface>().To<ExampleInterfaceImplementation>();
    }

    [Test]
    public void TestStandardResolving()
    { 
      // setup

      // business
      var result = kernel.Get<IExampleInterface>();

      // verify
      result.Should().NotBeNull();
      result.Should().BeOfType<ExampleInterfaceImplementation>();
    }

    [Test]
    public void TestPropertyResolving()
    {
      // setup
      var exampleClass = new ExampleClass();

      // business
      kernel.Inject(exampleClass);

      // verify
      exampleClass.ExampleProperty.Should().NotBeNull();
      exampleClass.ExampleProperty.Should().BeOfType<ExampleInterfaceImplementation>();
    }
  }

【问题讨论】:

  • 你真的要测试 Ninject 吗?!
  • 我只是想开始使用它而不是 Unity :) 不幸的是开始收到这个异常。
  • 您确实应该对自己的代码进行单元测试,而不是第三方工具。那么为什么不测试您使用的技术(asp.net、winforms、webforms、实体框架等)或整个 .NET 框架呢?如果您查看Ninject on github,您会发现该来源已经包含测试。
  • @archil 使用集成测试发现第三方接口是很常见的。

标签: .net nunit ninject code-coverage sharpdevelop


【解决方案1】:

SharpDevelop 使用PartCover 进行代码覆盖。当与使用 AllowPartiallyTrustedCallersAttribute 或类似的程序集一起运行时,这看起来可能与 known issue (见末尾)有关。

修复应该包含在最新的维护版本中,但我不知道 SharpDevelop 是否已打包最新版本。

【讨论】:

  • 不用担心 - 其他答案(批评您的测试方法)应该是 cmets。我实际上使用 Unity 和 Moq 来为我的测试创建一个自动模拟容器 - 所以我和 many others 也这样做。在测试方面,我们都有自己的偏好......
  • 今年我还将尝试将 OpenCover 添加到 SharpDevelop,因为它支持分支覆盖,并且正在积极开发中。
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多