gpt4 book ai didi

c# - 无法为引用可移植类库的程序集生成 Fakes

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:55 26 4
gpt4 key购买 nike

当类库引用 Visual Studio 2013 下针对 .Net 4.5 和 Silverlight 5 的 PCL(可移植类库)时,我无法为类库 (.NET 4.5) 生成 Microsoft Fakes 程序集。

这仅在类库 (.NET 4.5) 时发生:

  1. 声明从 System.dll v4.0.0.0 和 v2.0.5.0 中都可以找到的类型派生的类型
  2. 声明另一种派生自 PCL 库中声明的类型,而后者又派生自 System.dll v.2.0.5.0 中声明的类型

.NET 4.5 类库和 PCL 库都可以正常编译。但是我有一个单元测试项目(也是 .NET 4.5),它需要为 .NET 4.5 类库生成 Fakes。尝试生成假货失败并出现以下错误。

error : assembly Repro.dll failed to load properly Could not resolve assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'. Are you missing an assembly reference?

尝试在我的单元测试项目中添加对 System.Dll v2.0.5.0 的引用失败,因为对组件“System”v4.0.0.0 的引用已存在于项目中。

我还尝试将 App.Config 添加到我的单元测试项目并为 System.Dll 定义程序集绑定(bind)重定向,但没有成功。我怀疑程序集绑定(bind)重定向对假货生成的影响为零。

重现问题很容易。

创建一个名为“Repro.Portable”的可移植类库,它只有一个界面:

using System.ComponentModel; 

namespace Repro.Portable
{
public interface IPortableEntity : INotifyPropertyChanged
{
}
}

然后创建一个名为“Repro”的 .NET 4.5 类库,它引用可移植类库并声明以下两个接口(interface)。

using Repro.Portable;

namespace Repro
{
public interface ISpecializedEntity : IPortableEntity
{
}
}

和...

using System.ComponentModel;

namespace Repro
{
public interface IOtherInterface : INotifyPropertyChanged
{
}
}

最后,创建一个引用 Repro 和 Repro.Portable 程序集的单元测试项目。此时编译会成功。但是,尝试为 Repro 程序集生成 Microsoft Fakes 将失败。

由于所有这些看起来都是非常合理的事情,所以我想知道我是否遇到了 Microsoft Fakes 生成器中的错误,或者是否存在解决方法。

有什么方法可以使 Fakes 生成正常工作,还是我在遇到这种特定情况时只能使用不同的 Mocking 框架?

最佳答案

Microsoft 为该错误提供了解决方法。

To get past the issue, add an XML blob like below in your .fakes file.

<Compilation>
<Reference Path="C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile\Profile158\System.dll" FullName="System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"/>
</Compilation>

它在本地构建时确实有效,但是我还没有测试在使用 TFS 在线构建服务器时这是否是一个可行的解决方法。

关于c# - 无法为引用可移植类库的程序集生成 Fakes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22271705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com