counterfeiter

command module
Version: v0.0.0-...-590a33f Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: MIT Imports: 13 Imported by: 0

README

Counterfeiter

Build Status

When writing unit-tests for an object, it is often useful to have fake implementations of the object's collaborators. In go, such fake implementations cannot be generated automatically at runtime. This tool allows you to generate them before compilation.

Generating fakes

Choose an interface for which you would like a fake implementation:

$ cat path/to/foo/something.go
package foo

type Something interface {
	DoThings(string, uint64) (int, error)
	DoNothing()
}

Run counterfeiter like this:

$ counterfeiter path/to/foo Something
Wrote `FakeSomething` to `path/to/foo/foofakes/fake_something.go`

You can customize the location of the ouptut using the -o flag, or write the code to standard out by providing - as a third argument.

If you'd like a fake implementation for an interface you do not own you can do that too by providing only a fully qualified import path:

$ counterfeiter some/imported/package.Something
Wrote `FakeSomething` to `current/directory/fakes/fake_something.go`
Using the fake in your tests

Instantiate fakes with new:

import "my-repo/path/to/foo/foofakes"

var fake = new(foofakes.FakeSomething)

Fakes record the arguments they were called with:

fake.DoThings("stuff", 5)

Expect(fake.DoThingsCallCount()).To(Equal(1))

str, num := fake.DoThingsArgsForCall(0)
Expect(str).To(Equal("stuff"))
Expect(num).To(Equal(uint64(5)))

You can set their return values:

fake.DoThingsReturns(3, errors.New("the-error"))

num, err := fake.DoThings("stuff", 5)
Expect(num).To(Equal(3))
Expect(err).To(Equal(errors.New("the-error")))

You can set the return value of one or more specific calls:

fake.DoThingsReturnsOnCall(1, 3, errors.New("the-error"))

num, err := fake.DoThings("stuff", 5)
Expect(num).To(Equal(0))
Expect(err).NotTo(HaveOccurred())

num, err = fake.DoThings("stuff", 5)
Expect(num).To(Equal(3))
Expect(err).To(Equal(errors.New("the-error")))

You can also supply them with stub functions:

fake.DoThingsStub = func(arg1 string, arg2 uint64) (int, error) {
	Expect(arg1).To(Equal("stuff"))
	Expect(arg2).To(Equal(uint64(5)))
	return 3, errors.New("the-error")
}

num, err := fake.DoThings("stuff", 5)

Expect(num).To(Equal(3))
Expect(err).To(Equal(errors.New("the-error")))
License

Counterfeiter is MIT-licensed.

Documentation

The Go Gopher

There is no documentation for this package.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL