Raises
The Raises method is used to fire an event once a method is called. This topic goes through a number of scenarios where the Raises method is useful.
Assume we have the following interface:
public delegate void CustomEvent(string value, bool called);
public delegate void EchoEvent(bool echoed);
public delegate void ExecuteEvent();
public interface IFoo
{
event CustomEvent CustomEvent;
event EchoEvent EchoEvent;
event ExecuteEvent ExecuteEvent;
void RaiseMethod();
string Echo(string arg);
void Execute();
void Execute(string arg);
}Fire Custom Event on a Method Call
An example of how to use the Raises method to fire an event and pass event arguments once a method is called.
[TestMethod]
public void ShouldRaiseCustomEventOnMethodCall()
{
string actual = string.Empty;
bool isCalled = false;
// Arrange
var foo = Mock.Create<IFoo>();
Mock.Arrange(() => foo.RaiseMethod()).Raises(() => foo.CustomEvent += null, "ping", true);
foo.CustomEvent += (s, c) => { actual = s; isCalled = c; };
// Act
foo.RaiseMethod();
// Assert
Assert.AreEqual("ping", actual);
Assert.IsTrue(isCalled);
}Once the foo.RaiseMethod() is called the CustomEvent is raised with parameters "ping" and true.
Here is an another example for firing an event and passing event arguments once a method is called. Furthermore, we also mock the return value for the method.
[TestMethod]
public void ShouldRaiseCustomEventForFuncCalls()
{
bool echoed = false;
// Arrange
var foo = Mock.Create<IFoo>();
Mock.Arrange(() => foo.Echo("string")).Raises(() => foo.EchoEvent += null, true).Returns("bar");
foo.EchoEvent += (c) => { echoed = c; };
// Act
var actual = foo.Echo("string");
// Assert
Assert.AreEqual(foo.Echo("string"), "bar");
Assert.IsTrue(echoed);
}Once foo.Echo() is called with argument "string" the EchoEvent is raised with parameter true, echoed will be set to true as we attached the delegate specified above. In addition, the Echo method will return the string "bar" and we can verify that.
You can subscribe for an event more than once. Look at the following example:
[TestMethod]
public void ShouldAssertMultipleEventSubscription()
{
bool echoed1 = false;
bool echoed2 = false;
// Arrange
var foo = Mock.Create<IFoo>();
Mock.Arrange(() => foo.Execute()).Raises(() => foo.EchoEvent += null, true);
// Subscribing for the event
foo.EchoEvent += c => { echoed1 = c; };
foo.EchoEvent += c => { echoed2 = c; };
// Act
foo.Execute();
// Assert
Assert.IsTrue(echoed1);
Assert.IsTrue(echoed2);
}Both echoed1 and echoed2 will be set to true.
Fire Custom Event When Expectation Is Met
In this example we will use the same interface and will arrange that the ExecuteEvent must be raised only when the Execute method is called with an argument that matches the custom logic.
[TestMethod]
public void FireCustomEventWhenExpectationIsMet()
{
bool isCalled = false;
// Arrange
var foo = Mock.Create<IFoo>();
Mock.Arrange(() => foo.Execute(Arg.Matches<string>((s) => string.IsNullOrEmpty(s))))
.Raises(() => foo.ExecuteEvent += null);
foo.ExecuteEvent += delegate { isCalled = true; };
// Act
foo.Execute(string.Empty);
// Assert
Assert.IsTrue(isCalled);
}The ExecuteEvent is fired only when foo.Execute is called with empty string.
Fire Event With Lambda in EventArgs
For the next samples, we will use the following system under test:
public interface IExecutor<T>
{
event EventHandler<FooArgs> Done;
void Execute(string arg1, int arg2, bool arg3);
string Echo(string arg);
}
public class FooArgs : EventArgs
{
public FooArgs()
{
}
public FooArgs(string value)
{
this.Value = value;
}
public string Value { get; set; }
}In the first example we arrange that an event must be raised when method is called with any string, int and bool arguments. The EventArgs will be generated with lambda expression.
[TestMethod]
public void FireEventWithLambdaInEventArgs()
{
FooArgs args = null;
// Arrange
var executor = Mock.Create<IExecutor<int>>();
Mock.Arrange(() => executor.Execute(Arg.IsAny<string>(), Arg.IsAny<int>(), Arg.IsAny<bool>()))
.Raises(() => executor.Done += null, (string s, int i, bool b) => new FooArgs { Value = s + i + b });
executor.Done += (sender, e) => args = e;
// Act
executor.Execute("done", 3, true);
// Assert
Assert.AreEqual(args.Value, "done3True");
}When the executor.Execute method is called, an event is fired with Value property that equals to the concatenation of the string representations of the passed arguments.
And again, you can set a return value for the method as well. Lambda expressions are also allowed, thus the following example is acceptable:
[TestMethod]
public void FireEventWithLambdaInEventArgs2()
{
FooArgs args = null;
// Arrange
var executor = Mock.Create<IExecutor<int>>();
Mock.Arrange(() => executor.Echo(Arg.IsAny<string>()))
.Raises(() => executor.Done += null, (string s) => new FooArgs { Value = s })
.Returns((string s) => s);
executor.Done += (sender, e) => args = e;
// Assert
Assert.AreEqual(executor.Echo("echo"), args.Value);
}While we specify that the return value is the passed arguments, we can assert that args.Value and the result of the method call will both result in the argument we have passed.
Specify a Wait Duration
You already learned that the Raises method is used to fire an event once a method is called. However in some scenarios you might need a specific delay between the actual method call and the event that should be raised. Therefore, Raises allows you to specify this wait time duration as a function parameter.
What you have to do is to create an object implementing the IWaitDuration interface and pass it as a parameter to the Raises call. To create the duration object you can use one of the following static methods of the Telerik.JustMock.Wait class:
-
static IWaitDuration For(int seconds)
-
static IWaitDuration For(TimeSpan seconds)
Let's go through one useful example. Imagine that we have a Login class which uses ILoginValidationService and ILogger to perform user login validation. The IUserValidationService also has an event which will log a message using the ILogger manager when a CustomeEvent is fired (indicating that the user's credentials are validated):
public delegate void MyEvent(string value);
public interface ILogger
{
void LogMessage(string userName);
}
public interface IUserValidationService
{
bool ValidateUser(string userName, string password);
event MyEvent CustomEvent;
}
public class Login
{
ILogger Logger { get; set; }
IUserValidationService Validator { get; set; }
public TimeSpan ElapsedTime { get; set; }
Stopwatch stopWatch = new Stopwatch();
public Login(IUserValidationService validator, ILogger logger)
{
this.Logger = logger;
this.Validator = validator;
}
public bool LoginUser(string userName, string password)
{
stopWatch.Start();
Logger.LogMessage(userName);
var result = Validator.ValidateUser(userName, password);
stopWatch.Stop();
ElapsedTime = stopWatch.Elapsed;
return result;
}
}Now we want to arrange that the CustomEvent is fired a few moments after the validation process has ended. Here is how we can validate this type of scenarios:
[TestMethod]
public void ShouldWaitForSpecificDurationBeforeRasingTheEvent()
{
string userName = string.Empty;
string password = string.Empty;
// Arrange
var mockLogger = Mock.Create<ILogger>();
Mock.Arrange(() => mockLogger.LogMessage(userName)).OccursOnce();
var mockValidator = Mock.Create<IUserValidationService>();
Mock.Arrange(() => mockValidator.ValidateUser(userName, password))
.Raises(() => mockValidator.CustomEvent += null, userName, Wait.For(2))
.Returns(true);
// Act
var login = new Login(mockValidator, mockLogger);
// Assert
Assert.AreEqual(true, login.LoginUser(userName, password));
Mock.Assert(mockLogger);
Mock.Assert(mockValidator);
Assert.IsTrue(login.ElapsedTime.Seconds >= 1);
}We first create the ILogger and the IUserValidationService instances we need. We arrange that the ILogger.LogMessage method will occur only once. Furthermore, we arrange that when the IUserValidationService.ValidateUser method is called the CustomEvent will be fired in 2 seconds time. We are able to validate this using an ElapsedTime variable in the IUserValidationService which indicates the time gap between the validation and the time when the CustomEvent has been fired.