1 回答
TA贡献1906条经验 获得超10个赞
仅供参考:我认为微软的例子是错误的。一旦我将 View Component 更改为 View 并将数据从 View Component 代码中获取到后面并移到 Controller 中,那么一切都变得容易测试和实现 Bad Request 逻辑,等等。只需记住将 View Component 移出 View Component 目录,并在视图中将 layout 设置为 null。
控制器应该看起来更像这样:
public class MyController : Controller
{
private readonly IToDoContext _db;
public MyController(IToDoContext db)
{
_db = db;
}
public async Task<IActionResult> IndexVC("PriorityList", new { maxPriority, isDone = false })
{
if (string.IsNullOrWhiteSpace(maxPriority))
return BadRequest($"maxPriority cannot be empty");
var model = await InvokeAsync(maxPriority, isDone);
if (model == null)
{
return BadRequest($"model not found");
}
return View(model);
}
public async Task<IViewComponentResult> InvokeAsync(
int maxPriority, bool isDone)
{
var items = await GetItemsAsync(maxPriority, isDone);
return View(items);
}
private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
{
return _db.ToDo.Where(x => x.IsDone == isDone &&
x.Priority <= maxPriority).ToListAsync();
}
}
单元测试看起来像:
[Fact]
public void Should_return_viewcomponent()
{
var mockToDoContext = new Mock<IToDoContext>();
mockToDoContext.Setup(m => m.Get())
.ReturnsAsync(new ToDoContext());
var sut = new MyController(mockToDoContext.Object);
var result = await sut.PriorityList("myparams") as ViewResult;
result.Model.ShouldNotBeNull();
var resultModel = result.Model as PriorityListModel;
resultModel.ShouldNotBeNull();
resultModel.Thing.ShouldBe("whatever");
}
Bad Request 单元测试看起来像这样(注意 BadRequestObjectResult):
[Fact]
public async Task Should_return_bad_request_if_result_not_found()
{
var mockToDoContext = new Mock<IToDoContext>();
mockToDoContext.Setup(m => m.Get())
.ReturnsAsync(null);
var httpContext = new DefaultHttpContext();
var sut = new MyController(mockToDoContext.Object)
{
ControllerContext = new ControllerContext { HttpContext = httpContext }
};
var result = await sut.PriorityList("myparams") as BadRequestObjectResult;
result.ShouldNotBeNull();
result.StatusCode.ShouldBe(400);
result.Value.ShouldBe("model not found");
}
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报