matryer / moq

Interface mocking tool for go generate

Home Page:http://bit.ly/meetmoq

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't panic on nil callback?

abuchanan-nr opened this issue · comments

What if instead of panicing on a nil ...Func value, you just return the zero values?

Sometimes our tests don't need to return anything, we just want to capture and verify the calls. The mock.calls data structure is great for that, but adding empty implementations of ...Func adds boilerplate.

The question is whether people will be surprised by what's going on or not. What do you think?

I think it is an idea worth exploring, but if added to moq, it should be an opt-in feature which does not change the current default behavior.

See also #36

I would be open to a PR that added this as a flag. -defaults or similar.

I have a situation, where this feature would be beneficial for me, so I am interested in having this.

See also the (quite old) PR #96 for a potential implementation.

Came here looking for this. As far as I can see this hasn't been implemented yet, right?

Moq now supports a -stub flag to support this use-case. Example of generated code when flag is specified:

func (mock *QueuerMock) Sub(topic string) (<-chan Queue, error) {
callInfo := struct {
Topic string
}{
Topic: topic,
}
mock.lockSub.Lock()
mock.calls.Sub = append(mock.calls.Sub, callInfo)
mock.lockSub.Unlock()
if mock.SubFunc == nil {
var (
out1 <-chan Queue
out2 error
)
return out1, out2
}
return mock.SubFunc(topic)
}