Code Monkey home page Code Monkey logo

rest-in-peace's Introduction

Rest-in-peace

Build Status codecov

The aim of this project is to mock the microservices responses that your service depends on. By this way you will not need that third party services will be up and running in order to test and develop your microservice, because each time that you request some data to this APIs you will get a mock response.

Basic Usage

Install

http://gopkg.in/pjgg/rest-in-peace.v0

How to use

  1. Define MockServer port
const mockServerPort = 8080 
  1. Get a singleton instance of your MockServer
mockServer = Instance(mockServerPort)
  1. Clean your stubs each time that you run a test
mockServer.CleanStub()
  1. define your test stubs
outboundJSON := []byte(`{"key":"hello","value":"world"}`)
mockServer.When(GET, "/v1/service/hello*").ThenReturn(outboundJSON, 200)

Testify Integration example

This library needs from other libraries in order to build and orchestrate your unit/integration test. I will provide an integration example using testify.

  1. Define your test suit struct
type mockServerSuite struct {
	suite.Suite
	mockServer MockServerBehavior
	jsonAssert jsonAssert.JsonAssert
}
  1. Setup testify main function and instanciate your mockServer
const mockServerPort = 8080
func TestMockServerSuite(t *testing.T) {
	testSuit := new(mockServerSuite)
	testSuit.mockServer = Instance(mockServerPort)
	testSuit.jsonAssert = jsonAssert.Instance()
	suite.Run(t, testSuit)
}
  1. Define your test setup, the code that must be run before each test.
func (testSuit *mockServerSuite) SetupTest() {
	// Remember clean the stubs everytime that you run a test
	testSuit.mockServer.CleanStub()
}
  1. Write down your unit test
func (testSuit *mockServerSuite) TestHelloWorld() {
	var err error
	var resp *http.Response
	outboundJSON := []byte(`{"key":"hello","value":"world"}`)

	testSuit.mockServer.When(GET, "/v1/service/hello*").ThenReturn(outboundJSON, 200)

    // Your service as myService.method() or a simple httpRequest
    req, _ := newHTTPRequest("GET", "http://localhost:8080/v1/service/hello/", nil, nil)
    
    // assert your response.
	if resp, err = makeHTTPQuery(req); err == nil {
		data, _ := ioutil.ReadAll(resp.Body)

        // You could use restInPeace jsonAssert or testify asserts or your favorite assert lib
		assert.Nil(testSuit.T(), testSuit.jsonAssert.AssertJsonEquals(outboundJSON, data), "Unexpected error")
		assert.Equal(testSuit.T(), 200, resp.StatusCode)
	}

	if err != nil {
		testSuit.Fail("Unexpected error", err.Error())
	}

}

rest-in-peace's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mcikosos

rest-in-peace's Issues

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.