DEV Community

Andy Robinson
Andy Robinson

Posted on • Originally published at fresh-caffeine.com on

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.

Top comments (0)