How to test if raising an event results in a method being called conditional on value of parameters

Posted by MattC on Stack Overflow See other posts from Stack Overflow or by MattC
Published on 2010-03-29T17:06:49Z Indexed on 2010/03/29 17:13 UTC
Read the original article Hit count: 349

Filed under:
|
|
|
|

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);
        }
    }

© Stack Overflow or respective owner

Related posts about rhino

Related posts about mocks