It is actually pretty easy to raise events using the latest release of NMock. You can basically use Expect.Once.On(bla).EventAdd(“SomeEvent”, Is.Anything) to specify that you expect an event handler to be added for “SomeEvent” event on bla object and fire that event using Fire.Event(“SomeEvent”).
Here’s an example, minus any domain noise, that demonstrates the whole thing.
Let’s say there’s an interface IJoker which exposes an event JokeMade.
public interface IJoker
{
event EventHandler< JokeEventArgs > JokeMade;
}
public class JokeEventArgs : EventArgs
{
public string Joke { get; private set; }
public JokeEventArgs(string joke)
{
this.Joke = joke;
}
}
And there’s a class Batman which takes an object implementing IJoker as argument in its constructor.
public class Batman
{
private IJoker joker;
public Batman(IJoker joker)
{
this.joker = joker;
this.joker.JokeMade += (sender, e) =>
{
this.LatestJoke = e.Joke;
};
}
public string LatestJoke { get; private set; }
}
It hooks up a handler to the JokeMade event and updates the value of LatestJoke property with whatever joke is made.
So, here’s what the test would look like:
[TestMethod()]
public void BatmanConstructorTest()
{
using (Mockery mocks = new Mockery())
{
IJoker mockJoker = mocks.NewMock< IJoker >();
Expect.Once.On(mockJoker).EventAdd(“JokeMade”, Is.Anything);
Batman testBatman = new Batman(mockJoker);
string expectedJoke = “Bug-free code!”;
Fire.Event(“JokeMade”).On(mockJoker).With(mockJoker, new JokeEventArgs(expectedJoke));
Assert.AreEqual(expectedJoke, testBatman.LatestJoke);
}
}
Notice the call to EventAdd and Fire.Event.
That’s it, hope this helps.




