JustMock

The Raise method is used for raising mocked events. You can use custom or standard events.

Raising Custom Events

An example on how to use Raise to fire custom event.

C# Copy imageCopy
var foo = Mock.Create<IFoo>();

string expected = "ping";
string actual = string.Empty;

foo.CustomEvent += delegate(string s)
{
    actual = s;
};

Mock.Raise(() => foo.CustomEvent += null, expected);
Assert.AreEqual(expected, actual);

Here we use Raise to raise foo.CustomEvent and pass "ping" to it. Before acting we have attached a delegate to the event. Executing the delegate will result in assigning the passed string to actual. Finally, we verify that expected and actual have the same value.

Raising Standard Events

An example on how to use Raise to fire standard event.

C# Copy imageCopy
var executor = Mock.Create<IExecutor<int>>();

string actual = null;
string expected = "ping";

executor.Done += delegate(object sender, FooArgs args)
{
	actual = args.Value;
};

Mock.Raise(() => executor.Done+= null, new FooArgs(expected));
Assert.AreEqual(expected, actual);

Here we use Raise to raise a standard event - executor.Done accepting FooArgs object. The attached delegate sets the Value property in FooArgs object to the variable actual. Finally, we verify that expected and actual have the same value.

See Also