testing-with-gomock icon indicating copy to clipboard operation
testing-with-gomock copied to clipboard

Can you add instructions for where and how to write the DoSomething func?

Open eatwithforks opened this issue 2 years ago • 0 comments

Hi, where would you define the function for DoSomething? I tried adding it to doer.go and ran user_test.go with the stub commented out to see the test call the live function.

package doer

import "fmt"

//go:generate mockgen -destination=../mocks/mock_doer.go -package=mocks github.com/sgreben/testing-with-gomock/doer Doer

type Doer interface {
	DoSomething(int, string) error
}

func DoSomething(int, string) error {
	fmt.Println("doing stuff")
	return nil
}
func TestUse(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockDoer := mocks.NewMockDoer(mockCtrl)
	testUser := &user.User{Doer: mockDoer}

	// Expect Do to be called once with 123 and "Hello GoMock" as parameters, and return nil from the mocked call.
	//mockDoer.EXPECT().DoSomething(123, "Hello GoMock").Return(nil).Times(1)

	testUser.Use()
}

I get this error message:

    mock_doer.go:35: Unexpected call to *mocks.MockDoer.DoSomething([123 Hello GoMock]) at /Users/jyel/Code/public/testing-with-gomock/user/user.go:10 because: there are no expected calls of the method "DoSomething" for that receiver

Reason I'm asking this is because I understand the DoSomething stub works but want to see it go through the real function without the stub.

cc @sgreben

eatwithforks avatar May 16 '22 21:05 eatwithforks