-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed -mockNames not propagating to Recorder and ReturnCall types (#127)
When you are trying to mock multiple interfaces with the same name and similar methods (for example user.Service Create, post.Service Create, etc..) there is collision in Recorder and ReturnCall types, the -mockNames parameter only changes the main interface name, with the changes proposed in this PR the correct mockName will be applied to the Recorder and ReturnCall types. Addresses #117
- Loading branch information
Showing
10 changed files
with
257 additions
and
24 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package mock_names | ||
|
||
//go:generate mockgen -mock_names=Service=UserServiceMock -package mocks -typed -destination mocks/user_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/user Service | ||
//go:generate mockgen -mock_names=Service=PostServiceMock -package mocks -typed -destination mocks/post_service.go -self_package go.uber.org/mock/mockgen/internal/tests/mock_name/mocks go.uber.org/mock/mockgen/internal/tests/mock_name/post Service |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package mock_names | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/mock/gomock" | ||
"go.uber.org/mock/mockgen/internal/tests/mock_name/mocks" | ||
"go.uber.org/mock/mockgen/internal/tests/mock_name/post" | ||
"go.uber.org/mock/mockgen/internal/tests/mock_name/user" | ||
) | ||
|
||
func TestMockNames(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
|
||
userService := mocks.NewUserServiceMock(ctrl) | ||
postService := mocks.NewPostServiceMock(ctrl) | ||
|
||
gomock.InOrder( | ||
userService.EXPECT(). | ||
Create("John Doe"). | ||
Return(&user.User{Name: "John Doe"}, nil), | ||
postService.EXPECT(). | ||
Create(gomock.Eq("test title"), gomock.Eq("test body"), gomock.Eq(&user.User{Name: "John Doe"})). | ||
Return(&post.Post{ | ||
Title: "test title", | ||
Body: "test body", | ||
Author: &user.User{ | ||
Name: "John Doe", | ||
}, | ||
}, nil)) | ||
u, err := userService.Create("John Doe") | ||
if err != nil { | ||
t.Fatal("unexpected error") | ||
} | ||
|
||
p, err := postService.Create("test title", "test body", u) | ||
if err != nil { | ||
t.Fatal("unexpected error") | ||
} | ||
|
||
if p.Title != "test title" || p.Body != "test body" || p.Author.Name != u.Name { | ||
t.Fatal("unexpected postService.Create result") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package post | ||
|
||
import ( | ||
"go.uber.org/mock/mockgen/internal/tests/mock_name/user" | ||
) | ||
|
||
type Post struct { | ||
Title string | ||
Body string | ||
Author *user.User | ||
} | ||
|
||
type Service interface { | ||
Create(title, body string, author *user.User) (*Post, error) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package user | ||
|
||
type User struct { | ||
Name string | ||
} | ||
|
||
type Service interface { | ||
Create(name string) (*User, error) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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