这是我的控制器方法:
private IAutoManagerUnitOfWork _work;
public VehiclesController(IAutoManagerUnitOfWork work)
{
_work = work;
}
public async Task<ViewResult> Index()
{
return View("Index", await _work.VehicleRepository.GetAll().ToListAsync());
}
这是我的NUnit测试方法:
[Test]
public async Task Index_Get_ReturnsViewResult()
{
MockUnitOfWork uow = new MockUnitOfWork();
uow.VehicleRepository.Add(new Vehicle { Year = 2011, Make = "Ford", Model = "Mustang", Trim = "GT" });
var controller = new VehiclesController(uow);
ViewResult viewResult = await controller.Index();
var model = (List<Vehicle>)(viewResult.Model);
Assert.IsTrue(model.Count > 0); // Causes exception because model isn't defined.
}
当我单步执行测试时,viewResult.Model为空(Count = 0)。我试过让我的测试方法非异步并执行一个task.Wait()调用Index()之后,但我能让它工作的唯一方法是将我的控制器方法更改为非异步方法。我在Stack Overflow上找到了几个例子,但即便按照那些我无法使用它。我做错了什么?