Jason,
The problem doesn't exist with the event at all, the problem is with the
larger test, using the record/replay semantics. What's happening is that
behind the scenes is that there are a total of four expectations being set -
the constructor sets two (one on OnThingHappened and one on
SomethingElse()). The other two expectations are set explicitly by you when
you call mockOrderedThing.Expect...twice for FirstThing() and SecondThing().
Once in replay mode, you call DoThingsInOrder which calls SomethingElse(),
FirstThing(), SecondThing(), thus satisfying three of the four expectations.
The only unsatisfied expectation is the event - which as you've found is
never called.
Commenting out the call in the constructor removes the expectation - which
is why it passes.
To further illustrate that it's not the event, comment out the call in to
SomethingElse() in DoThingsInOrder(), you'll find the test fails.
What's really going on here is a problem with record/replay. Anything that
is called in record play is expected to be called later on, which the event
is never triggered. To get what you're going for I would suggest changing
the code to AAA syntax (a good suggestion in general) to the following:
[TestMethod]
public void TestThingsAreRunInOrderAAA()
{
// Arrange
IOrderedThing mockOrderedThing =
MockRepository.GenerateMock<IOrderedThing>();
MyTestClass classUnderTest = new MyTestClass(mockOrderedThing);
// Act
classUnderTest.DoThingsInOrder();
// Assert
mockOrderedThing.AssertWasCalled(thing => thing.FirstThing());
mockOrderedThing.AssertWasCalled(thing => thing.SecondThing());
}
On Mon, Sep 13, 2010 at 9:47 AM, Jason Specland <[email protected]> wrote:
> 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]<rhinomocks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.
>
>
--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz
--
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.