模型显示为空
本文关键字:显示 模型 | 更新日期: 2023-09-27 17:52:39
我正在写单元测试用例。我正在使用nunit和rhino mock。
我要测试的方法是
public ActionResult Details()
{
EmployeeDTO employee = this.EmployeeService.GetLoggedInEmployee();
EmployeeModel model = assembler.ToEmployeeModel(employee);
model.Title = GetEmployeeNameTitle(employee);
model.Controller = "LoanOfficer";
model.SelectedTab = MainNavTabs.LoanOfficerDetails;
return View(model);
}
和测试用例写在
[Test]
public void TestDetails()
{
EmployeeDTO employee = new EmployeeDTO();
EmployeeService.Stub(a => a.GetLoggedInEmployee()).Return(employee);
EmployeeModel model = new EmployeeModel{ Title = UtilityTests.Title, };
assembler.Stub(b => b.ToEmployeeModel(employee)).Return(model);
controller.Details();
// Assert
}
我已经做了
private ILoanModelAssembler loanAssembler;
loanAssembler = TestUtility.DynamicMock<ILoanModelAssembler>();
但仍然在这里模型是空的吗?在测试方法中有什么方法可以纠正吗?
它是空的,因为您没有从测试中控制EmployeeService的实际实例。如何将EmployeeService依赖项添加到控制器中?它是一个界面吗?您需要它是一个接口,以便Rhino mock可以模拟它,您还需要将它注入到控制器的构造函数中,以便将模拟实例注入到测试中。像这样:
private IEmployeeService employeeService;
public EmployeeController(IEmployeeService employeeService)
{
this.employeeService = employeeService;
}
然后在测试中你可以说:
var employeeServiceStub = MockRepository.GenerateStub<IEmployeeService>();
var employee = new EmployeeDTO();
employeeServiceStub.Stub(a => a.GetLoggedInEmployee()).Return(employee);