使用 MSBuild 时不会构建私有访问器

发布于 2024-08-02 14:29:33 字数 707 浏览 4 评论 0原文

我的构建服务器使用 MSBuild 来构建我的应用程序。我们的单元测试需要访问一些私有成员进行测试,因此我们使用内置的私有访问器。 Visual Studio 没有问题,但是当我们将代码推送到构建服务器时,我们收到错误:

MyTest.cs(96,13):错误CS0246: 类型或命名空间名称“My_Accessor” 找不到(您是否缺少 using 指令或程序集 参考?)

为什么 MSBuild 会忽略私有访问器,我该如何修复它?

我们使用 NUnit 作为测试框架,使用 CruiseControl.Net 作为持续集成服务器。

编辑: 根据评论,这里是存储库模式类的基类的一些测试代码。

MockRepository mocks = new MockRepository();

IDataContextWrapper wrapper = mocks.DynamicMock<IDataContextWrapper>();

Repository_Accessor target = new Repository_Accessor(wrapper);
Assert.AreEqual(wrapper, target._DataContext);

此代码只是验证成员变量 _DataContext 是否设置为模拟的包装器。在我的构建服务器上使用 MSBuild 构建时失败。

My build server uses MSBuild to build my application. Our unit tests require access to some private members for testing, so we use the built in private accessors. Visual Studio has no problem with it, but when we push our code to the build server we get the error:

MyTest.cs (96,13): errorCS0246: The
type or namespace name 'My_Accessor'
could not be found (are you missing a
using directive or an assembly
reference?)

Why is it that MSBuild ignores the private accessors and how can I fix it?

We use NUnit for our testing framework and CruiseControl.Net for our continuous integration server.

EDIT:
As per the comment, here is some test code for the base class for a repository pattern class.

MockRepository mocks = new MockRepository();

IDataContextWrapper wrapper = mocks.DynamicMock<IDataContextWrapper>();

Repository_Accessor target = new Repository_Accessor(wrapper);
Assert.AreEqual(wrapper, target._DataContext);

This code simply verifies that the member variables _DataContext is set to the mocked wrapper. It fails when building with MSBuild on my build server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

苏佲洛 2024-08-09 14:29:33

您的构建设置似乎没有生成私有访问器程序集。因此,您会遇到缺少 using 指令或程序集引用的错误。您可以查看 Publicize 工具来在构建过程中生成私有访问器过程。里面提到了

生成的程序集也是已知的
作为私有访问器。您还可以
从生成私有访问器
IDE,但您可以使用 publicize.exe
相反,在自动化、脚本编写和
构建场景。

我在一些测试中也使用私有访问器,并使用 TFS TeamBuild 和 MSTest。私有访问器是在构建期间生成的。我不需要手动执行任何操作。

It seems like your build setup is not generating the private accessor assembly. As a result, you're getting that missing using directive or assembly reference error. You can look at Publicize tool to generate the private accessor during your build process. It is mentioned in there that

The generated assembly is also known
as a private accessor. You can also
generate private accessors from the
IDE, but you might use publicize.exe
instead in Automation, scripting, and
build scenarios.

I use private accessors as well in some of the tests and use TFS TeamBuild and MSTest. Private accessor gets generated during builds. I did not have to do anything manually.

月依秋水 2024-08-09 14:29:33

您是否碰巧在本地正确设置了 [assemble:InternalsVisibleTo] 但在构建系统上设置错误?

(此外,Assert.AreSame() 可能更合适)。

Do you by chance have [assembly:InternalsVisibleTo] set correctly locally but incorrectly on your build system?

(Also, Assert.AreSame() might be more appropriate).

涫野音 2024-08-09 14:29:33

您提到了私有访问器,所以我假设您正在使用反射来公开一些私有成员以进行单元测试?

如果是这种情况,那么您必须了解,查看私有成员需要授予一定的权限。否则,流氓 .NET 程序可能会使用它想要的任何程序集来篡改任何内容。

您是否确定用户 Cruise Control 正在运行,并且随后构建运行程序和测试运行程序 CruiseControl 启动正在指定的帐户下运行“ReflectionPermission" 或来自 .NET 运行时的“完全信任”?

需要查找/谷歌的地方是构建服务器上的ma​​chine.config设置,Cruise Control自己的web.config(不确定它是否有一个,因为我已经很长时间没有使用CC了)和最后(也是最不可能的)是测试运行器和被测应用程序本身的 app.config 文件。

否则,我建议您使用 @Mehmet Aras 建议的 Publicize 工具。

You mentioned private accessors so I assumed that you are using Reflection to expose some private members for unit testing?

If that is the case, then you must understand that peeking into private members requires a certain rights to be granted. Or else, a rogue .NET program could tamper with anything it wants with whatever assemblies it wants.

Are you sure that the user Cruise Control is running under and subsequently, the build runner and test runner CruiseControl launches are running under an account that is given "ReflectionPermission" or "Full trust" from the .NET runtime?

Places to look/google for are the machine.config settings on the build server, Cruise Control's own web.config (not sure if it has one since I've not used CC for a long time) and lastly (and least probably), the app.config file of both the test runner and the application under test itself.

Otherwise, I suggest you use the Publicize tool as @Mehmet Aras suggested.

乱了心跳 2024-08-09 14:29:33

尽管 Mehmet 的回答很好,但恐怕让其正常工作的唯一方法是安装 Visual Studio。安装完成后,msbuild 开始为我生成访问器。这不是一个理想的解决方案,但至少我可以继续前进。

As good as Mehmet's answer was, I'm afraid the only way I could get this to work was to install visual studio. Once this was installed, msbuild started to produce the accessors for me. Not an ideal solution but at least I can move on.

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