如果使用私有访问器,针对 3.5 框架的 VS2010 SP1 单元测试会失败

发布于 2024-10-26 12:27:43 字数 1054 浏览 11 评论 0原文

我将解决方案从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-11-02 12:27:43

我也遇到了这个问题。 Visual Studio 2010 SP1增加了对基于.NET v3.5的单元测试项目的支持;以前的单元测试被迫使用.NET4。

有一个 关于该主题的 Microsoft Connect 错误,但它是在我撰写此答案的当天才提交的,因此 Microsoft 尚未做出有意义的回应。

我选择的解决方法是使用 Visual Studio 2008 工具链手动生成私有访问器程序集,并从单元测试项目添加对其的手动引用。

步骤如下:

1) 从单元测试 .csproj 文件中删除自动生成的访问器:

<ItemGroup>
  <Shadow Include="Test References\Assembly.accessor" />
</ItemGroup>

2) 使用 公开 VS2008:

"%VS90COMNTOOLS%vsvars32.bat"
publicize Assembly.dll

3) 将程序集复制到源树文件夹,例如文件夹 下访问器

copy Assembly_Accessor.dll ProjectDir\Accessors\Assembly_Accessors.dll

4) 使用 Visual Studio 界面添加访问器程序集作为对单元测试项目的引用:

Project -> Add Reference.. -> Browse...

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:

<ItemGroup>
  <Shadow Include="Test References\Assembly.accessor" />
</ItemGroup>

2) Create the v3.5 compatible accessor assembly using Publicize VS2008:

"%VS90COMNTOOLS%vsvars32.bat"
publicize Assembly.dll

3) Copy the assembly to the source tree folder, e.g. under a folder Accessors:

copy Assembly_Accessor.dll ProjectDir\Accessors\Assembly_Accessors.dll

4) Add the accessor assembly as a reference to the unit test project using the Visual Studio interface:

Project -> Add Reference.. -> Browse...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文