Skip to content
/ emock Public

Lightweight mocking library for external package calls in Go

License

Notifications You must be signed in to change notification settings

ebabani/emock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Lightweight mocking library for external package calls in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages