Hey there,

I'm having an problem when I try to wire up an event in one of my
classes inside the constructor whenever the "event" is part of my mock
object, and the tests I'm running are ordered.  It's easier to show
than to explain, so here's some sample code:

My sample class under test:

namespace RhinoMocksOrdredWithEvents {

    public interface IOrderedThing {
        void FirstThing();
        void SecondThing();
        void SomethingElse();
        event EventHandler<EventArgs> OnThingHappened;
    }

    public class MyTestClass {

        private IOrderedThing MyOrderedThing { get; set; }

        public MyTestClass(IOrderedThing orderedThing) {
            MyOrderedThing = orderedThing;
            MyOrderedThing.OnThingHappened += new
EventHandler<EventArgs>(MyOrderedThing_OnThingHappened);
            MyOrderedThing.SomethingElse();
        }

        public void DoThingsInOrder() {

            MyOrderedThing.SomethingElse();

            MyOrderedThing.FirstThing();
            MyOrderedThing.SecondThing();
        }

        private void MyOrderedThing_OnThingHappened(object sender,
EventArgs e) {
            return;
        }

    }
}


And my sample test:

namespace RhinoMocksOrdredWithEvents {
    [TestClass]
    public class OrderedMockTests {

        [TestMethod]
        public void TestThingsAreRunInOrder() {
            // Arrange
            MockRepository mocks = new MockRepository();

            IOrderedThing mockOrderedThing =
mocks.DynamicMock<IOrderedThing>();

            MyTestClass classUnderTest = new
MyTestClass(mockOrderedThing);

            using (mocks.Record()) {
                using (mocks.Ordered()) {
                    mockOrderedThing.Expect(thing =>
thing.FirstThing());
                    mockOrderedThing.Expect(thing =>
thing.SecondThing());
                }
            }

            mockOrderedThing.Replay();

            // Act
            classUnderTest.DoThingsInOrder();

            // Assert
            mockOrderedThing.VerifyAllExpectations();

        }
    }
}

When I run the tests, I get the following error:

Test method
RhinoMocksOrdredWithEvents.OrderedMockTests.TestThingsAreRunInOrder
threw exception:
Rhino.Mocks.Exceptions.ExpectationViolationException:
IOrderedThing.add_OnThingHappened(System.EventHandler`1[System.EventArgs]);
Expected #1, Actual #0..

Which is odd, since not only did I never say I expected the event to
be added, but the event was, in fact, added.

If I comment out the event wiring, the test passes.  If I wire the
event in the method rather than in the constructor, the test passes.

It seems like a bug to me, but I'm very hesitant to call it such when
it is far more likely to be the result of my own ignorance.  So what
am I missing?

-- Jason

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to