c# mstest 测试查询数据库在读取配置时抛出 FileNotFoundException 的方法
c# mstest test the method that query database throws a FileNotFoundException when it was reading config
我想创建一个测试项目并导入一个解决方案(解决方案 A),这样我就不需要在解决方案中添加测试项目。我想把测试项目和其他项目分开,因为我不想让 SVN 知道我已经创建了它。
当我测试一个通过 Entity Framwork 从数据库中查询数据的方法时。它抛出异常:
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.
我怎么了?我在 stack overflow 中搜索,并尝试从解决方案 A 中将配置复制到测试项目。但是它不起作用。
我找到了两个原因:
- 我使用.net 核心项目(测试项目)调用标准.net 框架项目中的方法。
- 所有测试项目都需要在Nuget中安装ConfigurationManager。
======================更新我上面的答案=================
我放弃了用mstest来做
我找到了另一种方法,但似乎有点stupid.I直接在控制器中写测试方法。
首先,声明一个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"
}
]
老实说,它并不好用,尤其是测试用例经常变化的时候。
我想创建一个测试项目并导入一个解决方案(解决方案 A),这样我就不需要在解决方案中添加测试项目。我想把测试项目和其他项目分开,因为我不想让 SVN 知道我已经创建了它。
当我测试一个通过 Entity Framwork 从数据库中查询数据的方法时。它抛出异常:
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.
我怎么了?我在 stack overflow 中搜索,并尝试从解决方案 A 中将配置复制到测试项目。但是它不起作用。
我找到了两个原因:
- 我使用.net 核心项目(测试项目)调用标准.net 框架项目中的方法。
- 所有测试项目都需要在Nuget中安装ConfigurationManager。
======================更新我上面的答案=================
我放弃了用mstest来做
我找到了另一种方法,但似乎有点stupid.I直接在控制器中写测试方法。
首先,声明一个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"
}
]
老实说,它并不好用,尤其是测试用例经常变化的时候。