我有一个看起来像的测试类:
// disclaimer: some type names have been changed to protect IP,
// there may be inconsistencies
using Moq;
using MyComp.MyProj.DataAccessLayer;
namespace Test.Common.Data.DataAccessLayer
{
public class Test
{
Mock<IApplicationData> appData;
Mock<IConfig> config;
public Test()
{
this.appData = new Mock<IApplicationData>();
this.config = new Mock<IConfig>();
}
[Fact]
public void GetNewInstance_WithoutUser( )
{
this.config.Setup(c => c.GetConfigInt(It.IsAny<string>())).Returns(1);
// DalFactory is a type in MyComp.MyProj.DataAccessLayer
var dal = DalFactory.GetDataAccessLayer(1, "fakestring", (IApplicationData)this.appData.Object, (IConfig)this.config.Object);
Assert.IsType <IDataAccessLayer>(dal);
}
}
}
```
这里的问题是,无论何时尝试访问DalFactory
类型,它都会抛出此异常:
System.TypeLoadException : Could not load type 'MyComp.MyProj.DataAccessLayer.DalFactory' from assembly 'DataAccessLayer, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null'.
版本是那里的关键,因为MyComp.MyProj.DataAccessLayer
在版本8.0.x的程序集中,而测试是在版本0.9.0(或1.0.0或0.0.1,我'我试过几个值)。
问题是,为什么Moq会尝试为这种类型加载错误的程序集?
我已经尝试过了:查看GAC是否有从那里加载的程序集,重新添加项目引用,在测试AssemblyInfo.cs中更改AssemblyTitle,更改测试所在的类的名称,在using
语句中使用别名。没有效果。
GetDataAccessLayer的方法是public static
,所以我不认为InternalsVisibleTo
因素在这里。如果我F12导航到正在测试的类型,它会很好,到达正确的位置。
如果我将Assert.True(1 == 1);
作为Test方法中的唯一内容,它运行正常并通过。
我应该尝试下一步来解决此问题?
答案 0 :(得分:0)
尝试:
看看是否有帮助
同时确保两个项目的客户端Target Framework相同(属性&gt;应用程序&gt;目标框架)