Ken Harris
2008-Apr-24 23:15 UTC
[mocha-developer] Mocking class methods: documentation suggestion
Hi Mocha people,
I''m just starting to use Mocha, and I had some trouble, so I thought
I''d share my experiences so other people don''t have to go
through what
I did.
New mocks worked just fine (a=mock(); a.expects(:b), ...) -- very
cool. But I wanted to mess with class methods, like Time.now, and it
was not happy with that for some reason:
$ irb
>> require ''mocha''
=> true
>> Time.stubs(:now).returns("pancakes")
NoMethodError: undefined method `stub'' for nil:NilClass
from
/opt/local/lib/ruby/gems/1.8/gems/mocha-0.5.6/lib/mocha/object.rb:57:in
`stubs''
from (irb):3
I found remarkably similar examples online (e.g.,
<http://www.floehopper.org/articles/2007/06/08/mocha-0-5-released>)
which don''t work here, either, which puzzled me. I ran
mocha''s tests
and they all passed.
So I dug through the source code, and discovered that some setup
methods need to get run before class methods will work, and they get
run automatically as part of unit tests, but not otherwise.
I would suggest some combination of (a) adding to the docs that mocks
only work as advertised inside a TestCase, (b) in object.rb:57, when
$stubba is nil, say something like "you need to run setup_mocha
first!", and/or (c) say in the docs what the setup method is, and how
to run it (maybe it''s there, but I didn''t see it).
But in any event, this message will soon be in google, so maybe that''s
enough.
cheers,
- Ken
James Mead
2008-Apr-25 08:43 UTC
[mocha-developer] Mocking class methods: documentation suggestion
Hi Ken, Thanks for taking the trouble to post to the mailing list (I''m sorry that your message got held up in the moderation queue because you didn''t subscribe - I keep thinking I should move the mailing list over to google-groups). This is something that someone else reported a while ago [1], so it is clearly a problem. I do remember changing some of the docs to try and improve things, but obviously not in the right place. Can you suggest anywhere specific that you would have noticed it? I like the idea of a better error message when setup hasn''t been run. I''ll look into that. It was intentional that the setup method wasn''t in the docs, because I thought that only test framework authors would need to know about it - I thought it might confuse other people. But perhaps if it was documented sensibly this need not be the case. Thanks. -- James. http://blog.floehopper.org http://tumble.floehopper.org [1] http://rubyforge.org/pipermail/mocha-developer/2007-July/000406.html 2008/4/25 Ken Harris <kengruven at gmail.com>:> Hi Mocha people, > > I''m just starting to use Mocha, and I had some trouble, so I thought > I''d share my experiences so other people don''t have to go through what > I did. > > New mocks worked just fine (a=mock(); a.expects(:b), ...) -- very > cool. But I wanted to mess with class methods, like Time.now, and it > was not happy with that for some reason: > > $ irb > >> require ''mocha'' > => true > >> Time.stubs(:now).returns("pancakes") > NoMethodError: undefined method `stub'' for nil:NilClass > from > /opt/local/lib/ruby/gems/1.8/gems/mocha-0.5.6/lib/mocha/object.rb:57:in > `stubs'' > from (irb):3 > > I found remarkably similar examples online (e.g., > <http://www.floehopper.org/articles/2007/06/08/mocha-0-5-released>) > which don''t work here, either, which puzzled me. I ran mocha''s tests > and they all passed. > > So I dug through the source code, and discovered that some setup > methods need to get run before class methods will work, and they get > run automatically as part of unit tests, but not otherwise. > > I would suggest some combination of (a) adding to the docs that mocks > only work as advertised inside a TestCase, (b) in object.rb:57, when > $stubba is nil, say something like "you need to run setup_mocha > first!", and/or (c) say in the docs what the setup method is, and how > to run it (maybe it''s there, but I didn''t see it). > > But in any event, this message will soon be in google, so maybe that''s > enough. >
Ken Harris
2008-Apr-25 21:15 UTC
[mocha-developer] Mocking class methods: documentation suggestion
Hi James,
A nice user-friendly error message would cover 99% of it. Truth be
told, people (including me, I''m not ashamed to say) often
don''t even
look at the docs until something goes wrong, and if it went wrong by
saying
"RuntimeError: you need to call mocha::setup_some_stuff if you
want to use mocks outside of a TestCase! --love, mocha.rb"
then the docs wouldn''t really have mattered.
Perhaps since the docs are largely by-example, we could just add
another example, "Using Mocha outside of TestCase". It would be about
5 lines long. :-)
- Ken
James Mead
2008-Apr-26 14:24 UTC
[mocha-developer] Mocking class methods: documentation suggestion
Cool. I''ve added a feature request [1] so it doesn''t get forgotten. -- James. http://blog.floehopper.org http://tumble.floehopper.org [1] http://rubyforge.org/tracker/index.php?func=detail&aid=19786&group_id=1917&atid=7480 2008/4/25 Ken Harris <kengruven at gmail.com>:> Hi James, > > A nice user-friendly error message would cover 99% of it. Truth be > told, people (including me, I''m not ashamed to say) often don''t even > look at the docs until something goes wrong, and if it went wrong by > saying > > "RuntimeError: you need to call mocha::setup_some_stuff if you > want to use mocks outside of a TestCase! --love, mocha.rb" > > then the docs wouldn''t really have mattered. > > Perhaps since the docs are largely by-example, we could just add > another example, "Using Mocha outside of TestCase". It would be about > 5 lines long. :-) > > > - Ken > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >