Logo

dev-resources.site

for different kinds of informations.

Simple Mocks With Mockaco

Published at
9/26/2024
Categories
dotnet
mockaco
mocks
integrationtesting
Author
freshcaffeine
Author
13 person written this
freshcaffeine
open
Simple Mocks With Mockaco

Mockaco is a simple yet powerful mocking framework for .NET that allows you to easily create and configure mock objects for your tests. It's designed to be lightweight and easy to use, with a simple syntax that makes it a breeze to set up your tests.

One of the things that sets Mockaco apart is its flexibility. It allows you to create mocks for any class or interface, and you can customise the behaviour of those mocks using a variety of methods. For example, you can set up a mock to return a specific value when a certain method is called, or you can throw an exception to simulate an error condition.

But Mockaco isn't just about making it easy to create mock objects. It also includes a variety of features to make your tests more powerful and flexible. For example, you can use Mockaco to verify that a specific method was called on a mock object, or to verify that a mock object was used in a specific way.

In one of my teams projects, we're using a Mockaco in a Docker container to mock an external API so we can run some integration tests in the ADO build pipeline.

Show me some code

docker-compose.yaml

version: "3.4"

services:
  mockaco.fresh-caffeine:
    image: natenho/mockaco
    ports:
      - '5000:5000'
    networks:
      - default
    volumes:
      - './mockaco/mocks:/app/Mocks'

Enter fullscreen mode Exit fullscreen mode
  • This Docker Compose file uses the natenho/mockaco image to create a container.
  • The ports section maps the container's port 5000 to the host's port 5000. This allows you to access the Mockaco web interface from your host machine.
  • The networks section specifies that the container should be attached to the default Docker network.
  • The volumes section mounts the ./mockaco/mocks directory on the host to the /app/Mocks directory inside the container. This allows you to store your mock object files on the host machine and access them from within the container.

And now for some simple mocks

This is a mock for a messaging api /v1/api/users/{loginId}/messages, where {loginId} can be any string.

Happy path mock

{
  "request": {
    "method": "POST",
    "route": "/v1/api/users/{loginId}/messages",
    "condition": "<#= 
      !String.IsNullOrEmpty(Request.Header["x-api-key"]?.ToString()) &&
      !String.IsNullOrEmpty(Request.Body["sender"]?.ToString()) &&
      !String.IsNullOrEmpty(Request.Body["body"]?.ToString()) 
    #>"
  },
  "response": {
    "status": "Created",
    "body": { 
      "messageId": "<#= Faker.Random.Guid() #>"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

For our happy path mock, we have three conditions set:

  • We require the request header to have an x-api-key and for it to be populated
  • We require the request body to include sender and body and for them to be populated In return, we get a 201 (Created) response with a body containing a randomly generated messageId

Unauthorized path mock

{
  "request": {
    "method": "POST",
    "route": "/v1/api/users/{loginId}/messages",
    "condition": "<#= 
      String.IsNullOrEmpty(Request.Header["x-api-key"]?.ToString())
    #>"
  },
  "response": {
    "status": "Unauthorized"
  }
}
Enter fullscreen mode Exit fullscreen mode

For the unauthorized mock, we catch requests where the header is missing a value for the x-api-key.

In return, we get a 401 (Unauthorized) response

Conclusion

As you can see, the setup is pretty simple. So if you're tired of manually creating and setting up mock objects for your .NET projects, give Mockaco a try! It's simple, flexible, and powerful, and it can help you write better tests in less time.

Go ready more about Mockaco on the GitHub repo.

integrationtesting Article's
30 articles in total
Favicon
Top 7 Best Integration Testing Tools for 2025
Favicon
Unit Test vs. Integration Test
Favicon
Choose Boring Releases
Favicon
The Struggle for Microservice Integration Testing
Favicon
Integration Testing in React: Best Practices with Testing Library
Favicon
Integration Testing in .NET: A Practical Guide to Tools and Techniques
Favicon
Testing Modular Monoliths: System Integration Testing
Favicon
Integration Testing : Concept
Favicon
The Complete Guide to Integration Testing
Favicon
End to End Testing vs Integration Testing โ€“ 7 Key Differences
Favicon
How to improve test isolation in integration testing using Jest and testcontainers
Favicon
๐Ÿš€ Effortless Integration Tests with Testcontainers in Golang ๐Ÿงช
Favicon
Testcontainers - Integration Testing Using Docker In .NET
Favicon
Simple Mocks With Mockaco
Favicon
Ace Your Tests with Mocha, Chai, Sinon, and the Clean Architecture
Favicon
Testing Made Easy with Ava: No More Pulling Out Your Hair!
Favicon
Nock: The Purr-fect Tool for Testing HTTP Interactions!
Favicon
Take Your Mocktail Game to the Next Level with Sinon.js: The Fun Way to Test Your Code
Favicon
Get Your Chai Fix and Learn the Art of Assertions at the Same Time
Favicon
Mocha Testing: The Java in Your Script
Favicon
The ultimate guide to Jest: turn your testing woes into rainbows and unicorns
Favicon
Get Your Test On with Jasmine: The Happy Way to Ensure Quality
Favicon
Experience the joy of stress-free coding with unit and integration testing!
Favicon
Boost Your Productivity with These Top-Notch JavaScript Testing Libraries! (2022)
Favicon
What is Integration Testing?
Favicon
Automated integration testing - what features help?
Favicon
When should integration testing be automated?
Favicon
Azure Functions E2E Testing within GitHub Actions
Favicon
Differentiate between integration testing and system testing
Favicon
Beginners' Introduction to React Testing

Featured ones: