Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

mokiat/gostub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoStub

Build Status Go Report Card

GoStub is a stubbing tool for the Go programming language.

You can use the tool to create stub implementations of your own Go interfaces. These stubs allow you to do the following things with them:

  • Check the number of times that a given method on the stub was called
  • Check the arguments that were used for a given call on the stub
  • Fake the implementation of a method on the stub with your own one

In that regard, this tool can be thought of a mocking tool as well. It is important to note that the generated stubs (mocks) are actual implementations of the interface and cannot be changed during runtime (unlike some frameworks in other languages that use reflection to change the behavior of the stubs).

The generated stubs are almost identical to the ones generated by the counterfeiter tool. I find that tool to be very helpful, however it has some issues (mostly in terms of type resolution) that would require a full redesign to solve and support seems to be limited nowadays. This motivated me to write the gostub tool.

User's Guide

You can get the tool using the following command. (You should have your Go development environment already set up)

go get github.com/mokiat/gostub

You can then navigate to the folder where your interface is located and execute the following command.

gostub <interface_name>

This will generate a stub called <interface_name>Stub in the <folder_name>_stubs/<interface_name>_stub.go file.

Note: The directory needs to be part of the src sub-tree of your $GOPATH.

It's unlikely that you will want to write that statement each time you seek to recreate your stub. Instead, you can use Go's generate functionality. Your interface file might look something like this.

package example

//go:generate gostub Person

type Person interface {
	// ...
}

All you need to do is run the go generate ./... command from the current or parent directory and all your stubs will be regenerated.

If you want to run the gostub from a different location, you can use the -s or --source flags to specify the location of the interface.

Example:

gostub -s $GOPATH/src/github.com/mokiat/example Person

If you wish to change to location where the generated stub will be saved, you can use the -o or --output flags to specify the file path of the generated stub.

Example:

gostub -o my_stubs/this_is_a_person_stub.go Person

If you wish to change the name of the generated stub, you can use the -n or --name flags to specify a new one. This will also affect the target file name, unless you use the -o or --output flags.

Example:

gostub -n StubbedPerson Person

Developer's Guide

This project uses the Ginkgo tool for the tests.

You can run all of the tests using the following command from the repository's root.

scripts/test.sh