How to test if raising an event results in a method being called conditional on value of parameters
- by MattC
I'm trying to write a unit test that will raise an event on a mock object which my test class is bound to.
What I'm keen to test though is that when my test class gets it's eventhandler called it should only call a method on certain values of the eventhandlers parameters.
My test seems to pass even if I comment the code that calls ProcessPriceUpdate(price);
I'm in VS2005 so no lambdas please :(
So...
public delegate void PriceUpdateEventHandler(decimal price);
public interface IPriceInterface{
event PriceUpdateEventHandler PriceUpdate;
}
public class TestClass
{
IPriceInterface priceInterface = null;
TestClass(IPriceInterface priceInterface)
{
this.priceInterface = priceInterface;
}
public void Init()
{
priceInterface.PriceUpdate += OnPriceUpdate;
}
public void OnPriceUpdate(decimal price)
{
if(price > 0)
ProcessPriceUpdate(price);
}
public void ProcessPriceUpdate(decimal price)
{
//do something with price
}
}
And my test so far :s
public void PriceUpdateEvent()
{
MockRepository mock = new MockRepository();
IPriceInterface pi = mock.DynamicMock<IPriceInterface>();
TestClass test = new TestClass(pi);
decimal prc = 1M;
IEventRaiser raiser;
using (mock.Record())
{
pi.PriceUpdate += null;
raiser = LastCall.IgnoreArguments().GetEventRaiser();
Expect.Call(delegate { test.ProcessPriceUpdate(prc); }).Repeat.Once();
}
using (mock.Playback())
{
test.Init();
raiser.Raise(prc);
}
}