[英]UnitTest a function with a DbContext as parameter
Hey I'm try to test this Function. 嘿,我试着测试这个功能。
Edit: 编辑:
The goal is to write a test that checks if the validate function works correct. 目标是编写一个测试,检查验证功能是否正常工作。
I'm just start with unit testing. 我刚开始进行单元测试。
I already tried just to mock the Context but this is not the solution. 我已经尝试过嘲笑上下文,但这不是解决方案。
It is not working or I made something wrong. 它没有用,或者我做错了。 ( Connection String error )
(连接字符串错误)
What is the correct approach for this Problem ? 这个问题的正确方法是什么?
Do you have any Idea ? 你有什么主意吗 ?
Thanks for your help. 谢谢你的帮助。
public static string validate(ProductDBEntities _db)
{
List<string> errorList = new List<string>();
foreach (var error in _db.GetValidationErrors())
{
errorList.Add(error.ValidationErrors.FirstOrDefault().ErrorMessage);
}
if (errorList.Count > 0)
{
string errors = string.Join("\n", errorList);
return errors;
}
return "";
}
The easiest solution to be able to unit test the logic inside validate would probably be: 能够在验证内部对逻辑进行单元测试的最简单的解决方案可能是:
public static string validate(List<SomeErrorMessageThing> validationErrors) //Whatever type var error is
{
List<string> errorList = new List<string>();
foreach (SomeErrorMessageThing error in validationErrors)
{
errorList.Add(error.ValidationErrors.FirstOrDefault().ErrorMessage);
}
if (errorList.Count > 0)
{
string errors = string.Join("\n", errorList);
return errors;
}
return "";
}
Then you don't have to mock anything, you could just send inn your manually crafted list of validationErrors from your test 然后你不必模拟任何东西,你可以从测试中发送手工制作的validationErrors列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.