我目前正在增加我们软件产品的代码覆盖率并且遇到了问题;由于抛出“BadImageFormatException”,我的所有单元测试(使用“任何 CPU”编译时)都失败了。
可以通过使用“x86”而不是“任何 CPU”构建解决方案来规避此异常,但是要求是我们需要能够使用任何 CPU/x64 位运行它们。
所有涉及 Moq 的单元测试都遵循几乎相同的格式:
[TestMethod]
public void GetProduct_ValidId_ProductReturned()
{
//Setting up the object
Product prod = new Product();
prod.ID = 7;
prod.Name = "Test";
//Create the mocks
var mockProductRepo = new Mock<IRepository<Product>>();
var testDb = new Mock<IUnitOfWork>();
//Setup what the repo needs to return, in this case it's a Product
mockProductRepo.Setup(m => m.getByID(7)).Returns(prod);
//Setup what the database needs to return, in this case it's our repo which we've already setup
testDb.SetupGet(m => m.ProductRepo).Returns(mockProductRepo.Object);
//Run the test
Product returnedProd = ProductHelper.getProduct(testDb.Object, 7);
Assert.IsNotNull(returnedProd);
}
我可以确认这个测试在使用 x86 构建时是成功的。有没有人知道如何让 Moq 在使用“任何 CPU”构建时发挥出色?
顺便说一句,我还可以确认我在解决方案中的所有项目都是使用相同的值(“任何 CPU”)构建的。我正在使用 Moq v4.0。
编辑:这是完整的异常:测试方法 [ProductName 和调用的方法] 抛出异常:System.BadImageFormatException:无法加载文件或程序集'[产品名称],版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = null'或其依赖项之一。试图加载格式不正确的程序。