Events

Moq has a feature that allows you to raise an event on the mock object. To raise an event, you use the Raise method. The method has two parameters. The first is a Lambda expression with the event subscribed for the event to be raised on the mock. The second argument provides a parameter that will be included in the event. To raise a LoanDefaulterNotification event on the loanRepository mock object with an empty argument, you can use the following line of code:

Mock<ILoanRepository> loanRepository = new Mock<ILoanRepository>();loanRepository.Raise(x => x.LoanDefaulterNotification+=null, EventArgs.Empty);

Real use cases are used when you want to have a mock object raise an event in response to an action or raise an event in response ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.