当单元测试位于不同的程序集中时,如何存根 Properties.Settings 对象?

发布于 2024-08-14 12:16:35 字数 173 浏览 11 评论 0原文

我有一个对象引用了一堆 Properties.Settings.Default... 值,我需要在该对象的单元测试中存根这些值。

不幸的是,设置对象的类型被声明为内部,因此我无法从单元测试项目访问它。

如何存根这些属性的返回值?我正在使用 Rhino Mocks 进行模拟。

I have an object the references a bunch of Properties.Settings.Default... values and I need to stub these in the unit test for this object.

Unfortunately the type for the settings object is declared as internal and so I can't access it from the unit test project.

How can I stub up the return values for these properties? I'm using Rhino Mocks for mocking.

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

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

发布评论

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

评论(3

把人绕傻吧 2024-08-21 12:16:35

一般来说,我不会。不要让“内部”对象实际读取 Properties.Settings.Default,而是让它们声明一个可配置的属性(在构造时或通过属性),并让另一段代码填充它们。

这样,您就可以在单元测试中测试除默认读取内容之外的所有内容,并且您与读取默认值的方式的耦合度会降低,从而使将来更容易切换机制。

In general, I wouldn't. Instead of your "inner" objects actually reading Properties.Settings.Default, have them declare a cconfigurable property (either at construction time, or via properties), and have another piece of code populate them.

That way, you can test everything but the default reading stuff in your unit tests, and you become less coupled to a way of reading the defaults, making it easier to switch the mechanism in the future.

我的黑色迷你裙 2024-08-21 12:16:35

您可能会发现对于解决内部问题有用的另一个技巧是,您实际上可以有选择地使内部代码对单元测试可见。在您正在测试的代码中,打开您的 AssemblyInfo.cs 并添加如下内容:

#if UNITTEST
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("YourUnitTestAssembly")]
#endif

然后您只需要在构建中定义 UNITTEST 符号,您的测试程序集将能够查看所有内部成员。

Also another tip that you may find useful to get around the internal problem, you can actually selectively make your internal code visible to your unit tests. In the code you are testing, open up your AssemblyInfo.cs and add something like the following:

#if UNITTEST
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("YourUnitTestAssembly")]
#endif

Then you just need the UNITTEST symbol defined in your build and your test assembly will be able to view all internal members.

溺渁∝ 2024-08-21 12:16:35

我知道这是一个老问题,但我想我会添加我的解决方案,因为它很简单并且可以帮助某人。

创建的设置类是一个分部类,我们可以利用它来创建我们自己的默认实现。

在Properties文件夹中创建一个新文件

internal partial class Settings : ISettings
{
    private static ISettings _setInstance;

    internal static ISettings Get
    {
        get
        {
            return _setInstance = _setInstance  ?? Default;
        }
        set { _setInstance = value; }
    }
}

然后当我们在应用程序的其他地方使用它时我们可以调用Settings.Get..
如果要设置测试中的值,请创建一个继承自 ISettings 的类并设置新的实现。

如果您的测试是在单独的项目中找到的,您将必须添加另一个类以将设置器公开给公众。
我们无法将设置文件更改为公共,因为下次我们更改或添加值时该文件将被覆盖。

I know this is an old question but I thought I would add in my solution as its simple and could help someone.

The settings class created is a partial class, we can leverage this to create our own implementation of Default.

Create a new file in the Properties folder

internal partial class Settings : ISettings
{
    private static ISettings _setInstance;

    internal static ISettings Get
    {
        get
        {
            return _setInstance = _setInstance  ?? Default;
        }
        set { _setInstance = value; }
    }
}

Then when we use it elsewhere in the application we can call Settings.Get..
If you want to set the values from test, create a class that inherits from ISettings and set the new implementation.

If your tests are found in a separate project you will have to add another class to expose the setter to public.
We cant change the settings file to public as this would just be overwritten the next time we change or add a value.

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