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

Allow mocking methods updating referenced structs #158

Merged
merged 1 commit into from
May 4, 2015

Conversation

ernesto-jimenez
Copy link
Member

@ernesto-jimenez ernesto-jimenez commented May 4, 2015

Some methods in Go take a pointer to a struct or a map and are supposed to set some values in the referenced argument. Using Return is not enough to mock such methods.

We are introducing a Run method that allows setting a Call handler to make your mock update such referenced values.

Example usage mocking a service that finds a user in a data store and fills the values in the provided struct:

m.On("Find", 1, mock.AnythingOfType("*User"))
  .Return(nil)
  .Run(func(args mock.Arguments) {
  u := args.Get(0).(*User)
  u.ID = 1
  u.Email = "[email protected]"
})

Some methods in Go take a pointer to a struct or a map and are supposed
to set some values in the referenced argument. Using Return is not
enough to mock such methods.

We are introducing a Run method that allows setting a Call handler to
make your mock update such referenced values.

Example usage mocking a service that finds a user in a data store and
fills the values in the provided struct:

```go
m.On("Find", 1, mock.AnythingOfType(*User))
  .Return(nil)
  .Run(func(args mock.Arguments) {
  u := args.Get(0).(*User)
  u.ID = 1
  u.Email = "[email protected]"
})
```
@ernesto-jimenez
Copy link
Member Author

@DAddYE, do you mind reviewing this?

Ideally it would be m.On(...).Run(...).Return(...), but it cannot be done right now due to the package internals, so we had to settle for doing m.On(...).Return(...).Run(...)

Thanks!

/cc @rnaveiras

@DAddYE
Copy link
Contributor

DAddYE commented May 4, 2015

Looks good to me! 👍

ernesto-jimenez added a commit that referenced this pull request May 4, 2015
Allow mocking methods updating referenced structs
@ernesto-jimenez ernesto-jimenez merged commit 73a8112 into master May 4, 2015
@ernesto-jimenez
Copy link
Member Author

Merged, thanks!

@ernesto-jimenez ernesto-jimenez deleted the mock-run-handler branch May 4, 2015 22:00
@dolmen dolmen added pkg-mock Any issues related to Mock mock.ArgumentMatcher About matching arguments in mock enhancement labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement mock.ArgumentMatcher About matching arguments in mock pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants