chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Lets say I have a interface I want to mock.



Public interface ICar

{

event EventHandler OutOfGas;

void Drive();

}



so I can test my Driver class



public class Driver

{

ICar _car;

bool _driving;



public Driver(ICar car)

{

_car = car;

_car.OutOfGas += new EventHandler(Car_OutOfGas);

_driving = false;

}



void Car_OutOfGas(object sender, EventArgs e)

{

_driving = false; // done driving
}



public void ZipAround()

{

_driving = true; // we are driving
}

public bool Driving

{

get { return _driving; }

}

}





so my test needs to…

[Test]

pubic void EventuallyRunOutOfGas()

{

ICar mockCar = GetMockFromSomeWhere();

Driver driver = new driver(mockCar);

driver.ZipAround();



// somehow, I have to tell the mock car to run out of gas….

mockCar.InvokeOutOfGasEventSomeHow();



Assert.IsFalse(driver.Driving);
}
asked by jwaldner (1.3k points)

8 Answers

0 votes
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.
answered by scott (32k points)
0 votes
will the new feature be part of the community edition and what is the timeframe? Thanks for all your help!! :P
answered by jwaldner (1.3k points)
0 votes
We haven't made the final plans for this feature yet.
Once we do we will give you all the information
answered by scott (32k points)
0 votes
Hi Scott,

Is there anything new on this.

Best regards,

Mads
answered by madsrode (180 points)
0 votes
Hi Mads,
We are working on this. Do you have a special urgent need?
answered by scott (32k points)
0 votes
No not at this moment. Was just interested.
answered by madsrode (180 points)
0 votes
Does v3.6 handle this scenario any better?
answered by alexdresko (1.7k points)
0 votes
answered by scott (32k points)
...