Understanding Method Invocation Verification with Moq's Verifiable Constructs

Tadit DashTadit Dash
2 min read

Introduction

I encountered a scenario where I needed to test if a method inside another method was actually invoked. This led me to learn about the Verifiable construct in the Moq library. ๐Ÿ’ป It provides a concrete way to ensure that the calls configured in the mock object are indeed invoked. โ„น

Example ๐Ÿš€

Suppose a CartsController has a CreateOrder method that calls the GetCartItems method within it:

public async Task<IActionResult> CreateOrder(OrderViewModel orderViewModel)
{
    var cartItems = await _cartService.GetCartItems(orderViewModel.CartId);

    // More codes...

    return View("OrderCreated");
}

A unit test for this method can be written to verify whether GetCartItems is actually invoked or not. Let's see how we can achieve this.

How to do? ๐Ÿคทโ€โ™€๏ธ

Mark the mock setup for the method as Verifiable and then call the Verify method on the mock object.

Here is a code snippet for one unit test case:

// Mock setup inside the unit test for the GetCartItems method.
_cartServiceMock.Setup(x => x.GetCartItems(It.IsAny<int>())).ReturnsAsync(() => cartItems).Verifiable();

// The following CreateOrder calls the GetCartItems inside it.
var result = await cartsController.CreateOrder(orderModel);

// Now we can verify
_cartServiceMock.Verify();

Notice that we set up a method GetCartItems to be Verifiable. Then, we called the method inside the CartsController to create an order. The CreateOrder method calls the GetCartItems method inside it.

So, _cartServiceMock.Verify(); will now be successful as it invoked the GetCartItems method.

If for some reason it does not, then the test will throw an exception like in the following screenshot.

Feedback

I hope it is clear. Let me know if you have any doubts. ๐Ÿ’ช Share, if you care. ๐Ÿ™Œ

Reference

  1. Moq GitHub Repository

  2. Moq Documentation on Verifications

0
Subscribe to my newsletter

Read articles from Tadit Dash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tadit Dash
Tadit Dash

As a ๐Ÿš€ Vice President with over 12 years of experience, I am a seasoned software architect known for designing and leading teams of engineers to deliver high-quality software products promptly and within budget. Throughout my career, I have spearheaded the end-to-end design of 7 innovative software products ๐ŸŽฏ. From conceptualization to deployment planning, I have successfully guided teams through requirements gathering, prototyping, testing, and deployment phases, ensuring exceptional outcomes. I take pride in my industry recognition, including being honored as a ๐Ÿ’Ž Microsoft Most Valuable Professional, ๐Ÿ’ก CodeProject Most Valuable Professional, and ๐Ÿ† DZone Most Valuable Blogger. Additionally, my expertise has been acknowledged by BookAuthority, which recognized my books on ASP.NET, REST API, Vue.js, and Dependency Injection as the ๐Ÿ“š best of all time. In addition to my professional achievements, I am passionate about mentorship and have been privileged to serve as a ๐ŸŒŸ Young Mentor at IndiaMentor, guiding aspiring professionals in their career journeys. For further information about my work and insights, please visit my website at ๐ŸŒ http://taditdash.com. You can also connect with me on ๐Ÿฆ Twitter at https://twitter.com/taditdash, ๐Ÿ‘ Facebook at https://www.facebook.com/taditdash, and ๐Ÿ’ผ LinkedIn at https://www.linkedin.com/in/taditdash. I am always open to networking and discussing opportunities, so feel free to reach out and connect. Let's explore how we can collaborate and drive innovation in the ever-evolving world of software architecture and development.