c# mstest测试查询数据库在读取配置时抛出FileNotFoundException的方法

我想创建一个测试项目并导入一个解决方案(解决方案 A),这样我就不需要在解决方案中添加一个测试项目。我想将测试项目和其他项目分开,因为我不想让 SVN 知道我已经创建了它。

当我测试通过实体框架从数据库中查询数据的方法时。它抛出一个异常:

Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=*************'. The system cannot find the file specified.

但是我检查了 Dependencies->SDK->Microsoft.NETCore App(2.1) ,我找到了 System.Configuration.dll。

我怎么了?我在堆栈溢出中搜索,并尝试从解决方案 A 复制配置以测试项目。但它不起作用。


婷婷同学_
浏览 244回答 1
1回答

明月笑刀无情

我找到了两个原因:我使用 .net 核心项目(测试项目)来调用标准 .net 框架项目中的方法。所有测试项目都需要在 Nuget 中安装 ConfigurationManager。====================== 更新我上面的答案 =================我放弃了使用 mstest 来做这件事。我找到了另一种方法,但似乎有点愚蠢。我直接在控制器中编写了测试方法。首先,声明一个TestBean。public TestBean(string name, string suppose, string fact, object msg = null){    this.name = name;    this.suppose = suppose;    this.fact = fact;    pass = suppose == fact;    if (SHOW_MSG)        this.msg = msg;    else        this.msg = null;}在控制器中使用这样的bean:[HttpGet]public string TestAll(){    JObject obj = (JObject)JsonConvert.DeserializeObject(TestMethod());    TestBean beans = new TestBean[]{        new TestBean('TestMethod',true+"",obj+"",obj)    };    return JsonConvert.SerializeObject(beans);}当我访问 url localhost:.../TestAll 时,我会得到 json[    {        "pass":true,        "name":"TestMethod",        "suppose":"True",        "fact":"True",        "msg":"True"    }]老实说,它并不容易使用,尤其是在测试用例频繁更改的情况下。
打开App,查看更多内容
随时随地看视频慕课网APP