我目前正在使用 rhino 模拟创建一些单元测试,并且我有一个如下所示的测试。但是我不断收到此错误消息,
IOsgController.AddWindow(Whiteboard.Model.OsgWindowProps); Expected #1, Actual #0.
这是由于 lambda 函数未在_dispatcher.BeginInvoke(()=>_osgController.AddWindow)
. 如何Action
在我的单元测试中执行它?
public void the_load_command_is_triggered_which_executes_the_load_control_method()
{
// arrange
IOsgController osgController = MockRepository.GenerateMock<IOsgController>();
IDispatcher dispatcher = MockRepository.GenerateMock<IDispatcher>();
Action action = osgController.AddWindow;
OsgViewModel osgViewModel = new OsgViewModel(osgController, dispatcher);
// dispatcher and add window should be called in LoadControl
dispatcher.Expect(d => d.BeginInvoke(action)).WhenCalled(i => action());
osgController.Expect(x => x.AddWindow());
// act
osgViewModel.LoadCommand.Execute(new object());
// assert
osgController.VerifyAllExpectations();
}
被测类是:
public class OsgViewModel : ViewModelBase
{
private readonly IOsgController _osgController;
private readonly IDispatcher _dispatcher;
public OsgViewModel(IOsgController osgController, IDispatcher dispatcher)
{
_osgController = osgController;
_dispatcher = dispatcher;
LoadCommand = new RelayCommand(LoadControl);
}
public ICommand LoadCommand { get; set; }
public void LoadControl()
{
// lambda is not being executed in unit test.
_dispatcher.BeginInvoke(
() => _osgController.AddWindow());
}
}