【问题标题】:Nsubstitute can't mock interfaces with signature with in parameterNsubstitute 不能用 in 参数模拟带有签名的接口
【发布时间】:2018-12-13 18:10:55
【问题描述】:

有这个接口:

public interface ICandyManager
{
     IReadOnlyList<string> GetCandies(in ReadOnlySpan<byte> bytes);
}

当我尝试使用 nsubstitute 模拟接口时:

ICandyManager nb = Substitute.For<ICandyManager>();

我收到以下错误:

Message: System.TypeLoadException : Signature of the body and declaration in a method implementation do not match.  Type: 'Castle.Proxies.ICandyManager'.  Assembly: 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

对此有什么解决方法?显然我可以初始化真实类型,但我希望能够模拟它。

【问题讨论】:

    标签: unit-testing nsubstitute


    【解决方案1】:

    我认为这是由于使用了 Castle.Core 的版本而不是 does not support reference semantics for value types

    目前我能想到的解决方法有两种:

    1) 将 NSubstitute 升级到 4.0.0 Release Candidate 1 或更高版本。这使用 Castle.Core 4.3.1(或更高版本)并与您问题中的界面正常工作。

    2) 继续使用 NSubstitute 3.1.0,但在您的测试项目中添加对更新的 Castle.Core NuGet 包的引用。我使用 NSubstitute 3.1.0 和 Castle.Core 4.3.1 对此进行了测试,并且我的示例测试开始正常运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多