I can see where you are going.
We actually do need to add features to make the scenario above easier.
Of course using TypeMock.NET this can be very simple.
:arrow: Use a
real implementation of ICar.
suppose you already have a mercedes:
public class Mercedes : ICar
{
...
public void OnOutOfGas(EventArgs e)
{
// Assign to a local variable to avoid race condition
EventHandler myEvent = OutOfGas;
if (myEvent != null)
{
myEvent(this,e);
}
}
Now in your tests you can use the real case.
[Test]
public void EventuallyRunOutOfGas()
{
MockObject mockCarControl = MockManager.MockObject(typeof(Mercedes));
// mock all methods that are not needed.
...
Mercedes car = mockCarControl.Object as Mercedes;
Driver driver = new driver(car);
driver.ZipAround();
// tell the car to run out of gas….
car.OnOutOfGas(null);
Assert.IsFalse(driver.Driving);
}
:arrow: If you still really want to use only an interface here is a way to do it untill we add this feature.
event EventHandler registeredEvent;
public bool SaveParameter(ParameterCheckerEventArgs
{
registeredEvent = (EventHandler)args.ArgumentValue;
return true;
}
[Test]
public void EventuallyRunOutOfGas()
{
MockObject mockCarControl = MockManager.MockObject(typeof(ICar));
// following line will call SaveParameter when event is added
mockCarControl.ExpectCall("add_OutOfGas").Args(new ParameterCheckerEx(SaveParameter));
ICar mockCar = mockCarControl.Object as ICar;
Driver driver = new Driver(mockCar);
driver.ZipAround();
// tell the mock car to run out of gas….
registeredEvent(this,null);
Assert.IsFalse(driver.Driving);
}
Hope this helps.