1

I am writting unit test cases. I am using nunit and rhino mock.

Method which is i am testing is

        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);
    }

And test case written is

        [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
    }

I have done

        private ILoanModelAssembler loanAssembler;
    loanAssembler = TestUtility.DynamicMock<ILoanModelAssembler>();

but still here model is null ? Is there is any way to correct it in test method?

4

1 回答 1

1

它是空的,因为您没有从测试中控制 EmployeeService 的实际实例。您的 EmployeeService 依赖项是如何添加到控制器中的?是接口吗?您需要它是一个接口,以便 Rhino Mocks 可以模拟它,并且您需要将它注入到控制器的构造函数中,以便您可以将模拟的实例注入到您的测试中。像这样的东西:

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);
于 2013-01-11T08:22:30.200 回答