- Knowledge for Developers.
Follow Us
Home Backend Mocking Test in .NET 7 example using Moq, xUnit, and Fluent Assertions

Mocking Test in .NET 7 example using Moq, xUnit, and Fluent Assertions

In this article, I present an example of using Mock Objects in .NET 7, based on a project created in previous versions of .NET and detailed in an article published some time ago on my blog:

Software Testing with .NET 5: usage examples (Portuguese)

The implementation in question used Moq as an alternative for handling Mock Objects, in addition to being based on xUnit and also using the Fluent Assertions framework. I’ve even made this example available on GitHub:

If you find this useful, please give a ⭐️ to support. I also invite you to follow me on GitHub!

Starting from the IServicoConsultaCredito interface:

And the StatusConsultaCredito enum and the Pendencia class, constructions used by the IServicoConsultaCredito interface: I’ve even made this example available on GitHub:

We will have the use these structures in the AnaliseCredito class:

And how could we then generate an object through the IServicoConsultaCredito interface without having an implementation of it?

The answer lies in the generic class Mock (namespace Moq), made available by Moq for creating Mock Objects. The following listing exemplifies all this, with the configuration of mappings in the constructor of type TestesAnaliseCredito and the use of a Mock Object simulating the possible conditions to be treated by AnaliseCredito:

This example was presented in detail in a recent live on Microsoft Reactor, with the content being able to watch for free on YouTube (in Portuguese):


MOQ | The most popular and friendly mocking framework for .NET

.NET + xUnit: Categorizing and Organizing Tests with Traits

*O conteúdo deste artigo é de responsabilidade do(a) autor(a) e não reflete necessariamente a opinião do iMasters.

Written by
Renato Groffe

Worked as a consultant in systems development activities for over 15 years. Microsoft MVP (Most Valuable Professional) and MTAC (Multi-Plataform Technical Audience Contributor) program participant. Bachelor in Information Systems, specialization in Software Engineering, and MBA in Business Intelligence. He is also a lecturer and technical author on portals and specialized magazines, focusing on Microsoft technologies and good practices in the software area.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles


How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...


The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....


EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...


Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...