Giter Club home page Giter Club logo

mock-it-up's Introduction

CI/CD

mock-it-up

Mock It Up is a mock server for testing purpose. It supports both static and dynamic stubs consist of http request/response.

Install

  • Install server
docker pull edentidus/mockitup
docker run -p 5000:5000 -p 30000:30000 edentidus/mockitup
  • Install client (C#)
Install-Package MockItUp.Client

Motivation

In microservice architecture, a serviec might denpendent to multiple other services by Restful Http requests. When doing acceptence/integration test, it is very annoying to connect to real downstream services. Trying to decoupe the dependencies, Mock It Up is a mock server to replace the real services.

img

Prepare for testing

  • Static stub should be registered as spec files before server started.
    version: v1
    stubs:
      - request:
          method: get
          path: api/orders/{id}
        response:
          body: order.json
          delay: 100
      - request:
          method: put
          path: api/orders
        response:
          statusCode: 201
          body: |
            {
                "id": 1000,
                "customerId": "${b.customer.id}"
            }
    For details, please refer to Static Stub Wiki.
  • Dynamic stub should be registered within a test scenario. It will be removed automatically when scenario disposed.
    using (var scenario = _client.BeginScenario())
    {
        var orderId = 215;
        // Register a dynamic stub to mock server
        var regResult = scenario.RegisterDynamicStub(stub =>
            stub.WhenRequest("GET", "api/orders/{id}")
                .RespondWith(JsonConvert.SerializeObject(new
                {
                    id = orderId,
                    title = "this is a test"
                }))
        );
    
        // Do your testing against real service
        var response = await httpClient.GetAsync($"{_orderUrl}/api/orders/{orderId}");
        var order = await ReadResponseAsync<dynamic>(response.Content);
    
        Assert.Equal(orderId, (int)order.id);
        Assert.Equal("this is a test", (string)order.title);
    
        // The stubs in current scenario will be removed in dispose
    
        // You can also manually remove any stubs within current scenario by
        // scenario.RemoveDynamicStubs(regResult.StubID);
    }
    For details, please refer to Dynamic Stub Wiki.
  • Both dynamic and static stubs can work together. Mock It Up try to match the dynamic stub, then static. Please run tests synchronously to aviod collision.
  • Before running docker containers, don't forget to copy your specs and payloads into container if you want to use static stubs.
  • Idealy, running acceptence/integration test requires three containers -- Service, Tests and Mock Server-- working together.
    • Run with docker-compose: docker-compose up --abort-on-container-exit --exit-code-from integrationtest.
    • Run in K8S: Include Mock It Up as a sidecar together with api service.
  • For more URL match examples, please refer to Tests.

Config the mock server

When start, Mock It Up reads settings via configuration file. Please ref to config.yml

Property Required? Default Value Notes
host required * Use 'localhost' in Windows for debugging
services required { *: 5000 } Add services by key:value (name:port) pair
controlPort required for dynamic 30000 dynamic only
specDirectory required for static /etc/mockitup.d/specs/ The url path to spec directory (static only)
payloadDirectory optional /etc/mockitup.d/payloads/ The url path to payload (definition of response bodies) directory (static only)

mock-it-up's People

Contributors

vincent-scw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

evadao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.