我安装了 VS2015 RTM 和 VS2013 Update 5 RTM。现在我的解决方案没有构建,因为我有一个返回类型为 X509Certificate2
的接口(interface).现在我的假货没有建立。我还创建了一个测试项目,在那里我遇到了同样的问题,所以这不是我的解决方案。我得到的信息是:
Cannot generate stub for ClassLibrary1.Interfaces.ICertificateProvider: method System.Security.Cryptography.X509Certificates.X509Certificate2 ClassLibrary1.Interfaces.ICertificateProvider.getbla() unstubbable: method is abstract and could not be stubbed, type System.Security.Cryptography.X509Certificates.X509Certificate2 is not available in the target framework version.
现在我卸载了VS2015 RTM,但问题仍然存在。当我用返回类型注释掉该方法时,证书一切正常。当我取消注释时,问题就在那里。
更新 1
我刚刚在另一个系统上测试了这个。首先,我使用 VS2013 Update 4 en VS2015 RC 进行了尝试。有了这个设置,一切都很好。然后我在该系统上安装了 Update 5 RTM,然后它就不再工作了。所以更新 5 一定是问题所在!
结束更新
重现:
使用 .Net Framework 4.5.1 创建具有 2 个类库和 1 个测试项目的解决方案。
在类库 1 中创建一个接口(interface)。
namespace ClassLibrary1.Interfaces
{
public interface ICertificateProvider
{
// Comment this line so you can build your fakes assembly...
X509Certificate2 getbla();
}
}
在第二个类库中创建一个类。实现接口(interface)并添加对第一个类库的引用。
namespace ClassLibrary1
{
public class CertificateProvider : ClassLibrary1.Interfaces.ICertificateProvider
{
public X509Certificate2 getbla()
{
throw new NotImplementedException();
}
}
}
在 unittest 项目中为 interface 项目添加一个 fakes 程序集。在测试中通过以下代码:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1.Interfaces.Fakes;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
StubICertificateProvider provider = new StubICertificateProvider();
}
}
}
现在您的项目将无法构建。如果您在界面中注释该方法,您的项目将构建。
在您的
.fakes
中启用诊断错误消息的文件。任何人的解决方案?
更新 2
将解决方案更改为使用 .Net Framework 4.6 有效。更改为 4.5.2 不起作用。
更新 3
Github官方bug链接:
https://github.com/dotnet/coreclr/issues/1303
最佳答案
我刚刚将测试项目更新为 .NET 4.6,效果很好。在那之后,我有几个内部类对假货来说是不可见的,所以使用
[组装:InternalsVisibleT
在 AssemblyConfig.cs 中让它最终运行也是一个好主意。
关于.net - System.Security.Cryptography.X509Certificates.X509Certificate2 在目标框架版本中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31668629/