Soaking events several times

In a specific unit test, I try to raise an event several times, and then to confirm the value of the property after the final event has been raised. I have something like

public void TurnRight() { var mockFoo = new Mock<IFoo>(); SomeService someService= new SomeService (); someService.Foo= mockFoo.Object; mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); mockFoo.VerifySet(foo=> foo.Orientation = Orientation.West); } 

The orientation actually only changed east (as I believe, the event only rises once). Am I doing something wrong? This is the first time I've used moq, so I probably missed something.

Cheers J

change ... the correct code that I had to use

 public void TurnRight() { var mockFoo = new Mock<IFoo>(); SomeService someService= new SomeService (); someService.Foo= mockFoo.Object; mockFoo.SetupProperty(foo=> foo.Orientation); mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); mockFoo.Raise(foo=> foo.TurnedRight += null, EventArgs.Empty); Assert.AreEqual(mockFoo.Object.Orientation, Orientation.South); } 
+7
c # unit-testing moq
source share
1 answer

mockFoo.Raise should be fine raising the event three times ... Can you put a breakpoint in the event handler and check how many times it is called?

Another potential error here, as I can see, is that you must first tell Moq to start tracking all the settings / properties of the object before you can check it (and before you raise events):

 // start "tracking" sets/gets to this property mockFoo.SetupProperty(foo=> foo.Orientation); 
+5
source share

All Articles