在单元测试中修改应用程序设置 [英] Modifying application settings in unit tests

查看:213
本文介绍了在单元测试中修改应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库我想使用微软的单元测试框架单元测试。有的我想测试的类所使用的应用程序设置进行配置。这些设置都具有应用范围和合适的默认值 Settings.settings 文件中定义。如果磁带库应用程序使用这些设置可以被覆盖在的app.config 文件。如果不是使用默认值。这正是我希望它是。

I have a class library I want to unit test using Microsofts unit test framework. Some of the classes I want to test are configured using application settings. These settings are defined inside the Settings.settings file having application scope and suitable default values. When the library is used by the application these settings can be overriden in the App.Config file. If not the default values are used. That's exactly how I want it to be.

在我的一些测试用例我想测试设定值的特殊组合,但我不知道如何从单元测试code下测试更改看到的类值。这些设置将始终从code生成的类的属性,有其默认值加载。

In some of my test cases I want to test special combinations of setting values but I don't know how to change the values seen by the class under test from the unit test code. These settings will always have their default value loaded from the attributes of the code generated class.

在我的库类我访问的设置是这样的:

In my library class I access the settings like this:

var mySetting1 = Settings.Default.MySetting1;
var mySetting2 = Settings.Default.MySetting2;

我如何修改的单元测试这些设置之前的设置是由类测试访问?通过单元测试使访问内部设置类没有解决不了的问题,因为设置有应用范围,并在设置类只读属性。

How do I modify these settings in an unit test before the setting is accessed by the class under test? Making the internal settings class accessible by the unit test does not solve the problem as the settings have application scope and are read-only properties on the settings class.

推荐答案

洞穴探险到 ApplicationSettingsBase 和相关的类,我想出了这个解决我的问题后。不是特别漂亮,但它肯定能够完成任务。

After spelunking into the ApplicationSettingsBase and associated classes I've come up with this solution to my problem. Not particular beautiful, but it certainly gets the job done.

在code产生的设置类是内部的类库项目,它必须是可访问的单元测试项目。添加 [总成:InternalsVisibleTo(UnitTestAssemblyName)。属性的AssemblyInfo.cs 在类库项目

The code generated settings class is internal to the class library project and it has to be accessible to the unit test project. Add the [assembly: InternalsVisibleTo("UnitTestAssemblyName")] attribute to AssemblyInfo.cs in the class library project.

的设置是延迟加载从上设置类的属性被访问的值时。第一步是做一个虚拟读取设置,迫使这一延迟加载。当单元测试你想避免改变了一个测试,以影响到另一个,因此有必要才延迟加载它们设置为复位的设置值。这可以通过使用刷新()方法来实现。这code被放置在测试初始化​​方法:

The settings are lazy loaded from the attributes on the settings class when a value is accessed. First step is to do a "dummy" read of a setting to force this lazy load. When unit testing you want to avoid settings values changed in one test to affect another hence it is necessary to "reset" the settings before lazy loading them. That can be done using the Reload() method. This code is placed in the test initialize method:

Settings.Default.Reload();
var dummy = Settings.Default.MySetting1;

背后的价值观,现在存在,并且可以在每个测试方法进行设置。请记住,使用正确的类型为code产生的吸气剂会做一个演员:

The underlying values now exist and can be set in each test method. Remember to use the correct type as the code generated getters will do a cast:

Settings.Default.PropertyValues["MySetting1"].PropertyValue = "Foobar";
Settings.Default.PropertyValues["MySetting2"].PropertyValue = 3.1416D;

这篇关于在单元测试中修改应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆