I learned RhinoMocks using the AAA syntax, so I'm not sure if you're setting things up properly or not with Record/Replay. However, I can tell you that if I use an AAA syntax to re-write what I think you want to test, it works. Here's an AAA version:
// arrange var communicator = MockRepository.GenerateStub<ICommunicator>(); var controller = MockRepository.GenerateMock<Controller>(communicator); // act communicator.Raise(c => c.IsConnected += null, null, EventArgs.Empty); // assert controller.AssertWasCalled(c => c.SendBlubberMessage()); This test makes sure the the controller's SendBlubberMessage was called after raising the IsConnected event. Does this help you out? --- Patrick Steele http://weblogs.asp.net/psteele On Mon, Apr 26, 2010 at 12:22 PM, Sebo <[email protected]> wrote: > Hi! > > I want to test whether a particular event of a dependency is fired, > and whether certain methods in the object are called. Both are free to > separate easily, but both does not work in a Test. I get the following > exception when creating the PartialMocks: > > > > Rhino.Mocks.Exceptions.ExpectationViolationException: > ICommunicator.add_IsConnected(System.EventHandler); Expected #1, > Actual #0. > ICommunicator.add_IsConnected(any); Expected #1, Actual #0. > > > > This is my Code and my Test: > > > public class Controller : IDisposable > { > private readonly ICommunicator communicator; > > public Controller(ICommunicator communicator) > { > this.communicator = communicator; > > ConnectToCommunicatorEvents(); > } > > internal void ConnectToCommunicatorEvents() > { > communicator.IsConnected += > Communicator_SendMessageSucceed; > } > > internal void Communicator_SendMessageSucceed(object sender, > EventArgs e) > { > SendBlubberMessage(); > } > > public virtual void SendBlubberMessage() > { > communicator.SendMessage("blubber"); > } > > public void Dispose() > { > communicator.IsConnected -= > Communicator_SendMessageSucceed; > } > } > > > public class Communicator : ICommunicator > { > private bool isConnected; > > public event EventHandler IsConnected; > > public bool Connect(string server) > { > ThrowAnExceptionIfAlreadyConnected(); > //do something > isConnected = true; > if (IsConnected != null) IsConnected(this, > EventArgs.Empty); > > return true; > } > > public bool Disconnect() > { > ThrowAnExceptionIfNotConnected(); > //do something > return true; > } > > void ICommunicator.SendMessage(string message) > { > ThrowAnExceptionIfNotConnected(); > //do something > } > > public virtual void ThrowAnExceptionIfAlreadyConnected() > { > if (isConnected) throw new Exception("already > connected!"); > } > > public virtual void ThrowAnExceptionIfNotConnected() > { > if (!isConnected) throw new Exception("not connected!"); > } > } > > [Test] > public void > If_Communicator_Event_IsConnected_Was_Fired_Controller_SendBlubberMessage_Is_Called() > { > IEventRaiser eventRaiser; > var mockRepository = new MockRepository(); > var communicatorDynamicMock = > mockRepository.DynamicMock<ICommunicator>(); > var controllerPartialMock = > mockRepository.PartialMock<Controller>(communicatorDynamicMock); > > using (mockRepository.Record()) > { > communicatorDynamicMock.IsConnected += null; > eventRaiser = > LastCall.IgnoreArguments().GetEventRaiser(); > > controllerPartialMock.SendBlubberMessage(); > LastCall.Callback(new Func<bool>(() => true)); > } > > using (mockRepository.Playback()) > { > eventRaiser.Raise(null, EventArgs.Empty); > } > } > > > > What am I doing wrong? > > -- > 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. > > -- 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.
