Created
November 26, 2016 12:41
-
-
Save seppestas/a56882d8eb6ad3c5aca815b45b376ab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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