2013-01-11 73 views
1

我在寫單元測試用例。我正在使用nunit和rhino mock。模型顯示null

方法這是我測試是

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

,但仍然在這裏模型是空?有什麼方法可以在測試方法中糾正它?

+0

*,其中*是空嗎? –

+0

你的代碼使用匯編程序,你的單元測試使用loanAssembler ... –

+0

嘿安德拉斯佐爾坦,我調試模型,然後它會顯示空。 – Ninad

回答

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