Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] "Don't mock value objects" - is it as important in duck-typed languages?

Pat Maddox

2008-01-13

Replies: Find Java Web Hosting

Author LoginPost Reply
This isn't about jmock specifically, but I figure this is as good as
any place to ask about using mocks.

I was reading "Test Smell: Everything is Mocked" [1] and J.B.'s
response "A Sign You're Mocking Too Much" [2]. Steve Says "It's not
worth creating an interface / implementation pair to control which
time values are returned, just create instances with the appropriate
values and use them," and J.B. reinforces that: "A value should be so
simple that there's no benefit to mocking them."

Looking at Steve's example, I understand why mocking something so
simple there would be bad. It's ugly and verbose. But that's about
all I can get from it. So what does the same test look like in Ruby?

show = Show.new
video1 = stub("video1", :time => 40)
video2 = stub("video2", :time => 23)

show.add video1
show.add video2
show.running_time.should == 63

which is really no different from using the real Video implementation,
which would change lines 2 and 3 to

video1 = Video.new 40
video2 = Video.new 23

So in Ruby, you don't save any typing or reading by using the real
implementation. You also get the benefit of isolation. If Video's
signature were to change to Video.new(title, running_time), then the
tests don't have to change.

One point I've glossed over is that Steve uses actual mocks by setting
expectations, and I just dynamically created a stub. I think that's
probably just a consequence of using JMock compared to RSpec's ability
to easily create stubs (also note that it's not an RSpec thing, I
could have used "OpenStruct.new(:time => 40)" and gotten the same
thing).

When you guys say it's not worth it to stub value objects, is that
just because it's comparatively messy? This is obviously a toy
example, but I'm having a hard time balancing the pros and cons of
using a mock vs real implementation in this case. On the surface, I
prefer using a mock because I'm coding to an interface, and it doesn't
cost me anything. At least I think it doesn't.

Finally, J.B. says "You should have no test doubles for values. If you
do, it's time to uncover what mislaid responsibility your value has,
extract it out, then try again." I don't really understand that, and
would appreciate some insight.

Cheers,
Pat



[1] http://www.mockobjects.com/2007/04/test-smell-everything-is-mocked.html
[2] http://jbrains.ca/permalink/90

---------------------------------------------------------------------
To unsubscribe from this list please visit:

  http://xircles.codehaus.org/manage_email

©2008 gg3721.com - Jax Systems, LLC, U.S.A.