gojuno / minimock

Powerful mock generation tool for Go programming language
MIT License
577 stars 38 forks source link

Implement a way to skip checking expectations for a mock object #38

Closed Kangaroux closed 5 years ago

Kangaroux commented 5 years ago

Based on #35.

It would be great if a mock could be skipped when the mock controller checks to see if expectations were met. This has several use cases:

Some possible naming schemes:

FooMock.ExpectAnything().Return("val")
FooMock.SkipExpectations()
FooMock.CaptureOnly()

ExpectAnything implies that the mock will be called at least once. SkipExpectations is the least vague IMO. CaptureOnly makes it sound like a Return() wouldn't work which probably isn't the desired functionality.

hexdigest commented 5 years ago

@Kangaroux

I just remembered that recently we changed the generated code in a way that Return works without preceeding Expect.

So

FooMock.ExpectAnything().Return("something")

Is already supported by the:

FooMock.Return("val")

Notation

So if there are no calls to Expect/When/Then there're no expectations to be met.

jesseroux commented 5 years ago

@hexdigest Ok great. Is that documented somewhere? I can't test it right now but I recall seeing an error like unexpected call to mock. I don't believe I set a Return() on that particular mock, though. So I assume it means that there needs to be at least a Return() on the mock?

hexdigest commented 5 years ago

@jesseroux yes there need to be at least a Return() call on the mock. I don't think that documentation explicitly says that you can call Return without Expect.

jesseroux commented 5 years ago

@hexdigest OK. I'll make a PR later to clarify that then

hexdigest commented 5 years ago

@jesseroux great! Can you please also mention Calls() method that you just added?

Kangaroux commented 5 years ago

@hexdigest Just to clarify about not needing an ExpectAnything method, because the current docs say that isn't how it works: https://godoc.org/github.com/gojuno/minimock#hdr-Keep_your_tests_clean

If the docs weren't updated when the Return() change was made I can just remove that section.

hexdigest commented 5 years ago

@Kangaroux can you please clarify what confuses you in the docs? What I wanted to say in this section is that Expect/Return/When/Then initialize a mock for some method and if you initialized a mock then MockController.Finish() ensures that this particular method has been called during the test.

The fact that you created an instance of the mock, i.e. readCloserMock := NewReadCloserMock(mc) isn't checked by Finish() but the compiler won't compile the test if there's no usage of readCloserMock.

hexdigest commented 5 years ago

@Kangaroux please follow this thread: https://github.com/gojuno/minimock/issues/30

Maybe new Inspect helper is enough for you?