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和VS2015 RC.有了这个设置一切都很好.然后我在该系统上安装了Update 5 RTM,然后它不再工作了.所以Update 5一定是问题所在!
结束更新
重现:
使用.Net Framework 4.5.1创建包含2个类库和1个测试项目的解决方案.
在类库1中创建一个接口.
namespace ClassLibrary1.Interfaces
{
public interface ICertificateProvider
{
// Comment this line so you can build your fakes assembly...
X509Certificate2 getbla();
}
}
在第二个类库中创建一个类.实现接口并添加对第一个类库的引用.
namespace ClassLibrary1
{
public class CertificateProvider : ClassLibrary1.Interfaces.ICertificateProvider
{
public X509Certificate2 getbla()
{
throw new NotImplementedException();
}
}
}
为unittest项目中的接口项目添加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的官方错误:
https://github.com/dotnet/coreclr/issues/1303
[assembly:InternalsVisibleT
在AssemblyConfig.cs中最好让它最终运行.