由于缺少 .config 文件而导致单元测试失败

发布于 2024-12-02 09:16:15 字数 419 浏览 3 评论 0原文

我是单元测试的新手,所以我可能误解了一些大的东西,但我被要求为我的 WCF 服务创建一些单元测试。这是一个非常简单的服务,执行存储过程并返回结果。我操作中的第二行是这样的:

string conn = ConfigurationManager
    .ConnectionStrings["AtlasMirrorConnectionString"].ConnectionString;

部署服务时一切正常,但在单元测试下,配置文件似乎变得不可见。 ConfigurationManager.ConnectionStrings["AtlasMirrorConnectionString"] 变为空引用并相应地抛出异常。

如何在测试中包含我的配置文件?现在,我能够测试的唯一行为是处理丢失的配置文件,这并不是很有用。

I am new to unit testing, so I am probably misunderstanding something big, but I have been asked to create some unit tests for my WCF service. It's a very simple service that executes a stored procedure and returns the result. The second line in my operation is this:

string conn = ConfigurationManager
    .ConnectionStrings["AtlasMirrorConnectionString"].ConnectionString;

Everything works fine when deploying the service, but under unit testing, it seems that the config file becomes invisible. ConfigurationManager.ConnectionStrings["AtlasMirrorConnectionString"] becomes a null reference and throws accordingly.

How do I include my config file in the tests? Right now, the only behavior I'm able to test is the handling of missing config files, which is not terribly useful.

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

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

发布评论

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

评论(3

濫情▎り 2024-12-09 09:16:15

一次又一次地问我,上周和本周我也回答了:)

如果你在另一个项目中进行单元测试(VS生成的测试项目、类库等...),只需为该单元测试创​​建一个应用程序配置项目并放置与您在有效项目中相同的配置密钥。

当然,我正在简化,因为您绝对可能想要使用特定的测试值自定义这些键,但作为开始复制有效的内容,然后进行自定义,以防您想指向另一个数据库、机器等...:)

asked again and again and again and answered by me last week and this week as well :)

if you have your unit tests in another project (VS generated test project, class library etc...) just create an app config for that unit test project and put the same configuration keys as you have in the project which works.

of course I am simplifying because you could absolutely want to customize those keys with specific test values, but as a start copy what works then customize in case you want to point to another database, machine etc... :)

从﹋此江山别 2024-12-09 09:16:15

如果您希望单元测试始终具有与项目相同的值,那么您可以使用以下行作为测试项目中的构建后事件

copy /Y "$(SolutionDir)ProjectName\App.config" "$(TargetDir)TestProjectName.dll.config"

If you want your unit test to always have the same values as your project then you can use the following line as a post-build event in the test project

copy /Y "$(SolutionDir)ProjectName\App.config" "$(TargetDir)TestProjectName.dll.config"
猫烠⑼条掵仅有一顆心 2024-12-09 09:16:15

您必须使用 DeploymentItemAttribute 将配置文件部署到测试目录中。

在您的 TestClass 上使用类似的内容(假设您有测试类本地 app.config 的副本):

[DeploymentItem("app.config")]

You'll have to decorate the test class or method with the DeploymentItemAttribute to deploy the configuration file into the test directory.

Use something like this on your TestClass (this assumes that you have a copy of the app.config local to your testclasses):

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