如果使用私有访问器,针对 3.5 框架的 VS2010 SP1 单元测试会失败
我将解决方案从VS2008转换为VS2010 SP1,并将单元测试项目更改为针对3.5框架。除了必须修复单元测试项目中的一些引用之外,一切正常并且解决方案构建成功。大多数测试都成功运行,但也有少数测试失败。失败的是使用私有访问器。就我个人而言,我宁愿删除这些测试,因为我认为它们没有必要,但只要它揭示了 SP1 中的潜在错误,我想我会看看是否有人能找到解决方法。
运行测试时收到的错误消息是“此程序集是由比当前加载的运行时更新的运行时构建的,无法加载。” 据我所知,私有访问器程序集似乎是由 4.0 运行时构建的(最有可能通过 Microsoft.VisualStudio.QualityTools.UnitTestFramework),但由于 3.5 运行时是由 MSTest 加载的,因此会发生错误。
我尝试更改 Microsoft.VisualStudio.QualityTools.UnitTestFramework 的引用以专门使用版本 9.0(当前为 10.1)。这会导致编译时错误,表明私有访问器程序集使用 Microsoft.VisualStudio.QualityTools.UnitTestFramework 版本 10.0,该版本高于版本 9.0。
我已删除生成的私有访问器程序集并重新创建它,但仍然存在相同的问题。当单元测试项目中以 3.5 框架为目标时,似乎有些东西与 VS2010 SP1 不同步。
这是其中一个单元测试的代码(同样,这不是一个非常有价值的测试,但这不是帖子的重点......):
[TestMethod()]
public void GetNullableCharValue_DBNull_ReturnsNull_Test()
{
object value = DBNull.Value;
Nullable<char> expected = null;
Nullable<char> actual;
actual = RepositoryBase_Accessor.GetNullableCharValue(value);
Assert.AreEqual(expected, actual);
}
I converted a solution from VS2008 to VS2010 SP1, and changed the unit test project to target the 3.5 framework. Other than having to fix a few references in the unit test project, everything worked ok and the solution built successfully. Most of the tests run successfully, but there were a handful that failed. The ones that failed are using a private accessor. Personally, I'd rather just remove these tests since I don't think they're necessary, but as long as it reveals a potential bug in SP1, I thought I'd see if anyone could figure out a work-around.
The error message that I receive when running the tests is "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."
As best as I can tell, it appears that the private accessor assembly is being built by the 4.0 runtime (most likely via Microsoft.VisualStudio.QualityTools.UnitTestFramework), but since the 3.5 runtime is loaded by MSTest, the resulting error occurs.
I tried changing the reference for Microsoft.VisualStudio.QualityTools.UnitTestFramework to specifically use version 9.0 (currently it is 10.1). This results in a compile time error which says that the private accessor assembly uses version 10.0 of Microsoft.VisualStudio.QualityTools.UnitTestFramework, which is higher than version 9.0.
I've deleted the generated private accessor assembly and recreated it as well, and still have the same issue. It would seem that something is out of sync with VS2010 SP1 when the 3.5 framework is targeted in a unit test project.
Here is the code for one of the unit tests (again, not a very valuable test, but that's not the point of the post...):
[TestMethod()]
public void GetNullableCharValue_DBNull_ReturnsNull_Test()
{
object value = DBNull.Value;
Nullable<char> expected = null;
Nullable<char> actual;
actual = RepositoryBase_Accessor.GetNullableCharValue(value);
Assert.AreEqual(expected, actual);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了这个问题。 Visual Studio 2010 SP1增加了对基于.NET v3.5的单元测试项目的支持;以前的单元测试被迫使用.NET4。
有一个 关于该主题的 Microsoft Connect 错误,但它是在我撰写此答案的当天才提交的,因此 Microsoft 尚未做出有意义的回应。
我选择的解决方法是使用 Visual Studio 2008 工具链手动生成私有访问器程序集,并从单元测试项目添加对其的手动引用。
步骤如下:
1) 从单元测试 .csproj 文件中删除自动生成的访问器:
2) 使用 公开 VS2008:
3) 将程序集复制到源树文件夹,例如文件夹
下访问器
:4) 使用 Visual Studio 界面添加访问器程序集作为对单元测试项目的引用:
5) 使用 Ctrl+Shift+B 构建解决方案并运行测试。
您现在可以签入生成的程序集或在预构建事件中自动创建它。
I ran into this problem as well. Visual Studio 2010 SP1 adds support for unit test projects based on .NET v3.5; previously unit tests were forced to use .NET4.
There is a Microsoft Connect bug on the subject but it was just filed the day I'm writing this answer so there is no meaningful response from Microsoft yet.
The workaround I chose was to manually generate the private accessor assembly using Visual Studio 2008 toolchain and add a manual reference to it from the unit test project.
The steps are:
1) Remove the autogenerated accessor from the unit test .csproj file:
2) Create the v3.5 compatible accessor assembly using Publicize VS2008:
3) Copy the assembly to the source tree folder, e.g. under a folder
Accessors
:4) Add the accessor assembly as a reference to the unit test project using the Visual Studio interface:
5) Build your solution with Ctrl+Shift+B and run your tests.
You can now either check in the generated assembly or create it automatically in a pre-build event.
当程序集经过签名和版本控制或存在访问器类时,此解决方案不起作用。
http://connect.microsoft.com/VisualStudio/feedback/details/677203/even-after-installing-vs2010-sp1-unit-tests-targeting-3 -5-framework-fail-if-they-are-using-private-accessor
This solution does not work when the asseblies are signed and versioned or when there is a accessor class.
http://connect.microsoft.com/VisualStudio/feedback/details/677203/even-after-installing-vs2010-sp1-unit-tests-targeting-3-5-framework-fail-if-they-are-using-private-accessor
尝试执行以下步骤:http://blogs.msdn.com/b/vstsqualitytools/archive/2010/12/19/how-to-re-target-单元测试到net-framework-3-5-in-vs-2010-sp1.aspx
Try following these steps: http://blogs.msdn.com/b/vstsqualitytools/archive/2010/12/19/how-to-re-target-unit-tests-to-net-framework-3-5-in-vs-2010-sp1.aspx