Coder Social home page Coder Social logo

minimock's Introduction

###Summary Minimock parses input Go source file containing interface declaration and generates implementation of this interface that can be used as a mock.

The main feature of Minimock is that you can reuse generated implementation in different test cases by attaching interface method implementations on the fly.

Let's say we have following interface declaration:

package sample

type Interface interface {
  GetString() string
}

For such interface generated implementation will look like:

type InterfaceMock struct {
  t *testing.T
  m *sync.Mutex

  GetStringFunc         func() string
  GetStringCounter      int
}

func (m *InterfaceMock) GetString() string {
  m.m.Lock()
  m.GetStringCounter += 1
  m.m.Unlock()

  if m.GetStringFunc == nil {
    m.t.Fatalf("Unexpected call to InterfaceMock.GetString")
  }

  return m.GetStringFunc()
}

func (m *InterfaceMock) ValidateCallCounters() {
  if m.GetStringFunc != nil && m.GetStringCounter == 0 {
    m.t.Error("Expected call to InterfaceMock.GetString")
  }
}

If caller performs a call to method that is not mocked the test case will fail. If caller does not perform a call to method that is mocked the test case will fail if you call to mock.ValidateCallCounters(). You can also perform more precise checks by using concrete call counters, i.e. mock.GetStringCounter

Please see more detailed example in examples subpackage.

###Usage of minimock:

  -f string
      input file containing interface declaration
  -i string
      interface name
  -o string
      destination file for interface implementation
  -p string
      destination package name
  -t string
      target struct name, default: <interface name>Mock

###Usage of minimock in go:generate instruction:

//go:generate minimock -f ./sample/interface.go -i Interface -o ./sample_interface_mock.go -p examples

minimock's People

Watchers

 avatar

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.