Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method names as constants #192

Closed
ghostiam opened this issue Apr 2, 2018 · 5 comments
Closed

Add method names as constants #192

ghostiam opened this issue Apr 2, 2018 · 5 comments

Comments

@ghostiam
Copy link

ghostiam commented Apr 2, 2018

Example:

	image := new(mocks.Image)
-	image.On("Add", mock.Anything)
+	image.On(mocks.ImageMethodAdd, mock.Anything)
type Image struct {
	mock.Mock
}

+ const (
+ 	ImageMethodAdd = "Add"
+ 	ImageMethodGet = "Get"
+ )

func (_m *Image) Add(image model.Image) (uint64, error) {}
func (_m *Image) Get(settings storage.SettingsImage) ([]model.Image, error) {}
...
@tutukin
Copy link

tutukin commented Jul 2, 2018

Elsewhere I saw a beautiful solution: mock has a special field, say "EXPECT" where all method control behavior is defined:

mock.EXPECT.Add(mock.Anything).Return()

@ghostiam
Copy link
Author

ghostiam commented Jul 2, 2018

@tutukin In this case, the restrictions are related to the library that is used for mocks
https://github.com/stretchr/testify#mock-package

For example, in another library implemented as:

phoneBook := NewMockPhoneBook()

// Stubbing:
pegomock.When(phoneBook.GetPhoneNumber(AnyString())).Then(func(params []Param) ReturnValues {
	return []ReturnValue{fmt.Sprintf("1-800-CALL-%v", strings.ToUpper(params[0]))}
},
// or
pegomock.When(phoneBook.GetPhoneNumber("Tom")).ThenReturn("345-123-789")

// Prints "1-800-CALL-DAN":
fmt.Println(phoneBook.GetPhoneNumber("Dan"))
phoneBook.VerifyWasCalledOnce().GetPhoneNumber("Tom")

@LandonTClipp
Copy link
Collaborator

I like this idea! I'll tag this for consideration...

@Gevrai
Copy link

Gevrai commented Jul 30, 2021

Elsewhere I saw a beautiful solution: mock has a special field, say "EXPECT" where all method control behavior is defined:

mock.EXPECT.Add(mock.Anything).Return()

Implemented a very similar solution to this in #396, although I had to make the EXPECT a method to get the underlying mock.

@LandonTClipp
Copy link
Collaborator

Closed as we now have .EXPECT()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants