There seem to be a bunch of tests that use mocks, but have unexpected uninteresting calls.I'd prefer people used NiceMock<> for these, because the (irrelevant) warnings often clutter up the build logs. Is that a good plan, or am I missing a reason that speaks against NiceMock?
There seem to be a bunch of tests that use mocks, but have unexpected uninteresting calls.I'd prefer people used NiceMock<> for these, because the (irrelevant) warnings often clutter up the build logs. Is that a good plan, or am I missing a reason that speaks against NiceMock?
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
On Tue, Aug 9, 2016 at 5:30 PM, Rachel Blum <gr...@chromium.org> wrote:There seem to be a bunch of tests that use mocks, but have unexpected uninteresting calls.I'd prefer people used NiceMock<> for these, because the (irrelevant) warnings often clutter up the build logs. Is that a good plan, or am I missing a reason that speaks against NiceMock?Not really an answer to your question, but I've rarely seen tests that seem better, more readable, less fragile, etc. to me using mocks than they would avoiding mocks (and using subclasses, dependency injection, registering as observers, etc.).So my personal bias is that rather than using NiceMock in some of these cases, we should see if we can avoid mocks entirely.
It is quite possible that I am speaking out of ignorance of good mocking practices here; however, I've seen this opinion expressed before by various others (Brett Wilson comes to mind).PK
--
I think the best rule of thumb is: avoid mocks if you can. If you can use subclasses, dependency injection, fakes, for_test methods, etc, go ahead. Mocking should be a last resort. But sometimes, testing that some code performs an exact sequence of operations, when the results of those operations do not leave any directly observable state, is best done with a mock.
A few months ago, I had a CL adding a test for graphical drawing, but gmock was banned in that particular part of the code base (ui/views). I was told by the reviewer that I couldn't use it but it was OK to record the drawing operations and then test that the expected operations were drawn correctly. So I did that. The result was that I essentially wrote my own small and crappy mocking toolkit with all the drawbacks of mocking but without the advantages of the years of engineering work on Gmock. IMO we ended up with much more complex, less readable tests, just so we could blindly follow a blanket "don't use Gmock, because mocking is bad" edict.
PK
--
On Wed, 10 Aug 2016 at 10:39 Peter Kasting <pkas...@chromium.org> wrote:On Tue, Aug 9, 2016 at 5:30 PM, Rachel Blum <gr...@chromium.org> wrote:There seem to be a bunch of tests that use mocks, but have unexpected uninteresting calls.I'd prefer people used NiceMock<> for these, because the (irrelevant) warnings often clutter up the build logs. Is that a good plan, or am I missing a reason that speaks against NiceMock?Not really an answer to your question, but I've rarely seen tests that seem better, more readable, less fragile, etc. to me using mocks than they would avoiding mocks (and using subclasses, dependency injection, registering as observers, etc.).So my personal bias is that rather than using NiceMock in some of these cases, we should see if we can avoid mocks entirely.I've run into this issue several times (it seems contentious on Chromium). I think it's harmful to have a blanket ban on mocking (which we have in some parts of the code base) or general advice to "avoid mocks entirely". Like many tools, mocking can be misused but there are cases where it is simply the right tool for the job. (Not getting into Gmock versus other mocking tools, just talking about mocking as a concept.)I think the best rule of thumb is: avoid mocks if you can. If you can use subclasses, dependency injection, fakes, for_test methods, etc, go ahead. Mocking should be a last resort. But sometimes, testing that some code performs an exact sequence of operations, when the results of those operations do not leave any directly observable state, is best done with a mock. Two major use categories come to mind: graphics drawing operations and network requests.A few months ago, I had a CL adding a test for graphical drawing, but gmock was banned in that particular part of the code base (ui/views). I was told by the reviewer that I couldn't use it but it was OK to record the drawing operations and then test that the expected operations were drawn correctly. So I did that. The result was that I essentially wrote my own small and crappy mocking toolkit with all the drawbacks of mocking but without the advantages of the years of engineering work on Gmock. IMO we ended up with much more complex, less readable tests, just so we could blindly follow a blanket "don't use Gmock, because mocking is bad" edict.
On Wed, Aug 10, 2016 at 3:52 AM Matt Giuca <mgi...@chromium.org> wrote:On Wed, 10 Aug 2016 at 10:39 Peter Kasting <pkas...@chromium.org> wrote:On Tue, Aug 9, 2016 at 5:30 PM, Rachel Blum <gr...@chromium.org> wrote:There seem to be a bunch of tests that use mocks, but have unexpected uninteresting calls.I'd prefer people used NiceMock<> for these, because the (irrelevant) warnings often clutter up the build logs. Is that a good plan, or am I missing a reason that speaks against NiceMock?Not really an answer to your question, but I've rarely seen tests that seem better, more readable, less fragile, etc. to me using mocks than they would avoiding mocks (and using subclasses, dependency injection, registering as observers, etc.).So my personal bias is that rather than using NiceMock in some of these cases, we should see if we can avoid mocks entirely.I've run into this issue several times (it seems contentious on Chromium). I think it's harmful to have a blanket ban on mocking (which we have in some parts of the code base) or general advice to "avoid mocks entirely". Like many tools, mocking can be misused but there are cases where it is simply the right tool for the job. (Not getting into Gmock versus other mocking tools, just talking about mocking as a concept.)I think the best rule of thumb is: avoid mocks if you can. If you can use subclasses, dependency injection, fakes, for_test methods, etc, go ahead. Mocking should be a last resort. But sometimes, testing that some code performs an exact sequence of operations, when the results of those operations do not leave any directly observable state, is best done with a mock. Two major use categories come to mind: graphics drawing operations and network requests.A few months ago, I had a CL adding a test for graphical drawing, but gmock was banned in that particular part of the code base (ui/views). I was told by the reviewer that I couldn't use it but it was OK to record the drawing operations and then test that the expected operations were drawn correctly. So I did that. The result was that I essentially wrote my own small and crappy mocking toolkit with all the drawbacks of mocking but without the advantages of the years of engineering work on Gmock. IMO we ended up with much more complex, less readable tests, just so we could blindly follow a blanket "don't use Gmock, because mocking is bad" edict.^^^ This.I think it hurts test readability when each test harness re-invents this particular wheel differently. I am opposed to a blanket ban on GMock.