emock

package module
v0.0.0-...-4d70331 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2015 License: MIT Imports: 2 Imported by: 0

README

emock

Lightweight mocking library for function calls in Go

The purpose of this library is to allow easy mocking of function calls, and ease dependency injection.

Get it:

go get github.com/ebabani/emock

Docs

http://godoc.org/github.com/ebabani/emock

Mocking:

Example

The following example uses ginkgo and gomega for assertions

myFunc = func(a int, b string) string {
	return fmt.Sprintf("A IS %+v B is %+v", a, b)
}

It("should return an empty string", func() {
  mockObj = MockFunc(&myFunc) // (1)
  defer mockObj.Restore() // (2)
  
  ret := myFunc(123, "Cool stuff") // (3)
  
  Expect(mockObj.GetArgsForCall(0)).To(MatchArgs(123, "Cool stuff")) // (4)
  Expect(mockObj.CallCount()).To(Equal(1)) // (5)
  Expect(ret).To(BeEmpty()) // (6)
})
1 - Create a mock object

Mock a function at the given address, and return a mock object. The mock object can be used to check how many times the mocked function was called, and with what arguments. (See lines 4 and 5)

2 - Restore the original function implementation

Restore the function to its normal behaviour instead of the mock implemetation.

3 - Call the function.

When mocked, by default the function will return zero values, but you can also set custom return values.

4 - Check the args of the first call to myFunc.

GetArgsForCall(i) will return the args used in the i'th call to the mocked function. The args are returned as an []interface{}

MatchArgs is a custom gomega matcher to make it easier to check if the function was called with the right args.

5 Call count of the mocked function

How many times the mocked function was called.

6 Check the return value of the mocked function

By default when something is mocked it will return zero values for all its returns. (See https://golang.org/ref/spec#The_zero_value)

You can use mockObj.SetReturns to set custom returns, or mockObj.SetReturnFunc to replace the mocked function with another one with the same signature.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mock

type Mock struct {
	Calls [][]interface{}
	// contains filtered or unexported fields
}

If the function being mocked returns a func, that function will be nil

func MockFunc

func MockFunc(funcAddress interface{}) *Mock

Mock the function at the specified address.

By default the function will return zero values.

func (*Mock) CallCount

func (self *Mock) CallCount() int

Returns the number of calls made to the mocked function.

func (*Mock) GetArgsForCall

func (self *Mock) GetArgsForCall(n int) interface{}

Returns the args for the n'th call to the mocked function

func (*Mock) Restore

func (self *Mock) Restore()

Restore the original behaviour of the function that was being mocked.

func (*Mock) SetReturnFunc

func (self *Mock) SetReturnFunc(function interface{})

Replace the mocked function with the provided one. Panics if the given function signature does not match the original one.

func (*Mock) SetReturns

func (self *Mock) SetReturns(returns ...interface{})

Set what the mocked function should return. Panic if the number of return values doesn't match the original function, or if the types don't match

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL