Telerik® JustMock™ by Progress

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:

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.

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.

Once the foo.Echo() is called with argument "string" the EchoEvent is raised with parameter true. This will set echoed to be 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:

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.

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:

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.

When 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:

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 learnt 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 users basic 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):

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:

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.

See Also

Other Resources

[b9461116-b200-4739-aff1-af8458c7095e]