使用Rhino Mock进行单元测试

时间:2011-11-09 22:23:48

标签: .net unit-testing rhino-mocks

[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。问题是什么?感谢。

1 个答案:

答案 0 :(得分: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
// ..

有关详细信息,请参阅Rhino Mocks Properties