C# 如何确定在方法中测试什么
我不熟悉单元测试。我正在努力解决的一件事是确定 为我的方法编写什么类型的测试。使用下面的代码作为 例如,我应该在这里测试什么?我能做的各种测试有哪些 写C# 如何确定在方法中测试什么,c#,unit-testing,c#-4.0,xunit,C#,Unit Testing,C# 4.0,Xunit,我不熟悉单元测试。我正在努力解决的一件事是确定 为我的方法编写什么类型的测试。使用下面的代码作为 例如,我应该在这里测试什么?我能做的各种测试有哪些 写 [HttpGet] [路线(“GetPosts”)] 公共异步任务GetPosts() { 尝试 { var posts=await postRepository.GetPosts(); if(posts==null) { 返回NotFound(); } 返回Ok(posts); } 捕获(例外) { 返回请求(); } } 在单元测试中,您测
[HttpGet]
[路线(“GetPosts”)]
公共异步任务GetPosts()
{
尝试
{
var posts=await postRepository.GetPosts();
if(posts==null)
{
返回NotFound();
}
返回Ok(posts);
}
捕获(例外)
{
返回请求();
}
}
在单元测试中,您测试一小段代码,例如方法。单元测试只测试要测试的方法中的代码,这一点很重要。它不测试任何依赖项。他们必须被嘲笑(例如,用最低起订量)
我认为您在这里处理的是三个测试用例(=三个测试):
[HttpGet]
[Route("GetPosts")]
public async Task<IActionResult> GetPosts()
{
try
{
var posts = await postRepository.GetPosts();
if (posts == null)
{
return NotFound();
}
return Ok(posts);
}
catch (Exception)
{
return BadRequest();
}
}