2011-11-09 86 views
0
[Test] 
public void OnActionExecuting_Always_Call_CheckStatisticActionAuthorization() 
{ 
    //Arrange 
    var _ActionExecutingContext = GetActionExecutingContext(); 
    var _StatisticController = MockRepository.GenerateStub<StatisticsController>(); 

    _StatisticController.DataContext = fDataContext; 

    //Act 
    _StatisticController.OnActionExecuting(_ActionExecutingContext, false); 

    //Assert 
    _StatisticController.AssertWasCalled(aStatisticController => 
      aStatisticController.CheckStatisticActionAuthorization(_ActionExecutingContext)); 
} 

我一直從CheckStatisticActionAuthorization獲取NullReferenceException,StatisticController.DataContext爲null。問題是什麼?謝謝。使用Rhino Mock進行單元測試

+1

Rhino Mocks Properties哪裏'fDataContext'從何而來?你沒有在你提供的樣本中的任何地方創建它。 –

+0

'StatisticsController'的相關實現細節是什麼? – harlam357

回答

0

試用的DataContext屬性指定PropertyBehavior()

var statisticController = MockRepository.GenerateStub<StatisticsController>(); 
statisticController.Expect(m => m.DataContext).PropertyBehavior(); 
statisticController.DataContext = fDataContext; 

// check whether it set properly 
Assert.AreEqual(fDataContext, statisticController.DataContext); 
Assert.IsNotNull(statisticController.DataContext); 

// Act 
// ... 

// Assert 
// .. 

更多細節