I'm trying to mock an interface's events like the following:
[TestMethod]
public void NeedingDataFiresEvents()
{
//Arrange
var service = MockRepository.GenerateMock<IService>();
service.Expect(i => i.GetValue()).Return(5开发者_JAVA百科);
var view = MockRepository.GenerateMock<ILogView>();
view.NeedData += null;
LastCall.IgnoreArguments();
var evt = LastCall.GetEventRaiser();
var presenter = new LogPresenter(view, service);
var args = new DataEventArgs();
//Act
evt.Raise(view, args);
//Assert
Assert.AreEqual(1, args.Results.Count());
}
The error I'm getting is: System.InvalidOperationException: Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).
I'm not sure why... what am I doing wrong? What would I apply virtual too, if I'm not actually instantiating the view... Something in the presenter?
OK, figured it out, this was the answer, to use the MockRepository instance (an example, sample below taken from there):
var fooDatabaseMock = new MockRepository();
var fooDatabase = fooDatabaseMock.DynamicMock<IFooDatabase>();
fooDatabase.FooDatabaseInsertEvent += null;
LastCall.IgnoreArguments();
var fooEventRaiser = LastCall.GetEventRaiser();
精彩评论