app.configs 和 MSTest 项目 - 连接字符串的空引用 [英] app.configs and MSTest Project - null reference for a connection string

查看:21
本文介绍了app.configs 和 MSTest 项目 - 连接字符串的空引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行单元测试 (mstest) 时,我遇到了这个问题.
代码行:

When I try to run Unit Tests (mstest) I run into this issue.
The line of code:

_mainCnStr = System.Configuration.ConfigurationManager.
               ConnectionStrings["main"].ConnectionString;

作为空引用返回

当我运行它时,它不会在主 UI 项目中执行此操作.获取单元测试项目看到的此连接字符串设置的正确方法是什么?我试过作为资源嵌入.我试过永远复制.什么是可以为我解决此问题的正确设置组合?

It doesn't do this in the main UI project when I run it. What is the right method to get this connection string setting seen by the Unit Test project? I tried embedded as a resource. I tried Copy Always. What is the right combination of settings that will fix this for me?

推荐答案

MSTest 需要注意的一件事(至少来自 IDE);它不在常规输出 (bin) 文件夹中运行测试,也不尊重项目的文件包含(复制到输出目录").您经常需要明确地告诉它 (MSTest) 将哪些文件放入测试区.您需要在此列表中包含app.config";通过 testrunco​​nfig(部署"),或通过添加属性([DeploymentItem]) 到受影响的测试装置.

One thing to watch with MSTest (from the IDE at least); it doesn't run the tests in the regular output (bin) folder, and it doesn't respect the project's file inclusions ("Copy to Output Directory"). You often need to explicitly tell it (MSTest) which files to put into the test area. You will need to include the "app.config" in this list; either via the testrunconfig ("Deployment"), or by adding an attribute ([DeploymentItem]) to the affected test fixtures.

这篇关于app.configs 和 MSTest 项目 - 连接字符串的空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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