Skip to content

Instantly share code, notes, and snippets.

@seppestas
Created November 26, 2016 12:41
Show Gist options
  • Save seppestas/a56882d8eb6ad3c5aca815b45b376ab7 to your computer and use it in GitHub Desktop.
Save seppestas/a56882d8eb6ad3c5aca815b45b376ab7 to your computer and use it in GitHub Desktop.
package stuff
import (
"github.com/stretchr/testify/mock"
"testing"
)
// Interface to mock
type Stuff interface {
DoThings(fu, bar string) (string, error)
}
// Function under test
func DoThingsWithStuff(s Stuff) (string, error) {
return s.DoThings("hello", "world")
}
// Test code
type MockedStuff struct {
mock.Mock
}
func (m *MockedStuff) DoThings(fu, bar string) (string, error) {
args := m.Called(fu, bar)
return args.String(0), args.Error(1)
}
// Causes index out of range
func TestDoThingsWithStuffDoesThingsWithoutArgs(t *testing.T) {
mocked_stuff := new(MockedStuff)
mocked_stuff.On("DoThings").Return("did things", nil)
DoThingsWithStuff(mocked_stuff)
}
// Correct
func TestDoThingsWithStuffActualyDoesWithArgs(t *testing.T) {
mocked_stuff := new(MockedStuff)
mocked_stuff.On("DoThings", mock.Anything, mock.Anything).Return("did things", nil)
DoThingsWithStuff(mocked_stuff)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment