.NET 单元测试 - 如何测试 applicationSettings

发布于 2024-08-20 08:19:47 字数 285 浏览 2 评论 0原文

我有一个类库,它使用应用程序设置来确定特定方法的行为。

我意识到有人可能会认为这是一件坏事,但我们知道该网站的行为将在启动后大约一周发生变化(没有真正的确定时间),并且最简单的方法是更改​​设置在配置文件中更改站点的行为。

无论如何,关于我的问题...我希望能够测试一种方法,该方法的行为根据 applicationSetting 的 true/false 值而变化。如何创建单元测试来测试单元测试的 app.config 中未定义的值?该设置是一个应用程序设置,因此它在 Settings.Default 实例中是只读的。

I have a class library that is using application settings to determine the behavior of a particular method.

I realize that the argument could be made that this is a Bad Thing, but we know that the behavior of the site is going to change about a week after launch (no real definitive time), and it will be easiest to just change the setting in the config file to change the behavior of the site.

Anyways, on to my question... I would like to be able to test a method who's behavior changes based on the true/false value of an applicationSetting. How do I create a unit test to test the value that is not defined in the unit test's app.config? The setting is an Application setting, so it is read-only in the Settings.Default instance.

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

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

发布评论

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

评论(2

堇年纸鸢 2024-08-27 08:19:47

如果您为ApplicationSettings,您可以在运行时替换您的测试存储库,并在测试期间对设置执行任何您想要的操作。

If you create a repository for the ApplicationSettings, you can substitute your test repository at runtime, and do whatever you want with the settings during test.

回首观望 2024-08-27 08:19:47

您测试系统中发生更改的部分在将值传递给它时是否行为正确。您 App_Start 从配置文件中提取值并将它们推送到系统的特定部分,而不是让它本身提取配置值。这样,您的单元测试就可以实际测试行为,而无需修改配置文件。

You test that the part of the system that changes behaves correctly when the values are passed to it. You the App_Start pull the values from the config file and push them to the specific part of the system, not have it pull the config values itself. That way your unit tests can actually test the behavior without mucking with the config file.

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