Hi guys. I’m setting an expectation on a class method. For some
reason, the method isn’t being stubbed, and the expectation isn’t
seeing the call.
In KeywordListsController#create, I call the private method #create_keywords_and_associate, which calls AdSenseHeavenParser#parse
.
Thus, some of my specs set an expectation on AdSenseHeavenParser#parse
. However, that expectation isn’t stubbing out #parse .
In the “output” section of my paste: http://pastie.org/488160
you can see that the second line is “*** create_keywords_and_associate
***”. However, that string shouldn’t be printed if
AdSenseHeavenParser#parse is stubbed out.
I’ve been beating my head against this for a couple of hours. Any
thoughts?
I’ve been beating my head against this for a couple of hours. Any
thoughts?
The easiest for you is to use the debugger and go through each line in
the controller.
Maybe @keyword_list.save is returning false? Stub it out and see by
yourself.
On Sun, May 24, 2009 at 12:49 PM, Fernando P. [email protected]
wrote:
I’ve been beating my head against this for a couple of hours. Any
thoughts?
The easiest for you is to use the debugger and go through each line in
the controller.
Maybe @keyword_list.save is returning false? Stub it out and see by
yourself.
Hi Fernando. In this case, I don’t think it’s a matter of using the
debugger.
I’ve already stubbed out #save on @keyword_list, and confirmed that
AdSenseHeavenParser#parse is being called. I know the latter is
occuring because this is being displayed when the spec is run:
*** create_keywords_and_associate ***
If AdSenseHeavenParser#parse was actually stubbed out, or wasn’t
called at all, then that line wouldn’t be displayed.
On Sun, May 24, 2009 at 2:13 PM, Fernando P. [email protected]
wrote:
Hi Fernando. In this case, I don’t think it’s a matter of using the
debugger.
I suspect a problem in: require ‘lib/adsense_heaven_parser’, the code
maybe never gets out of this require statement. The debugger would allow
you to immediately clear things out.
Hi Fernando. I’m not sure what you mean by “the code never gets out of
this require statement”.
However, that doesn’t really matter. The call to #require shouldn’t
happen, because the method is supposed to be stubbed out.
Hi Fernando. In this case, I don’t think it’s a matter of using the
debugger.
I suspect a problem in: require ‘lib/adsense_heaven_parser’, the code
maybe never gets out of this require statement. The debugger would allow
you to immediately clear things out.
Hi Fernando. I’m not sure what you mean by “the code never gets out of
this require statement”.
I should have said ‘the interpreter never …’
However, that doesn’t really matter. The call to #require shouldn’t
happen, because the method is supposed to be stubbed out.
You are making a lot of suppositions that are not obvious to us. Nowhere
in your pastie is your require call stubbed.
Hi Fernando. I’m not sure what you mean by “the code never gets out of
this require statement”.
I should have said ‘the interpreter never …’
Ah, I understand what you mean now.
However, that doesn’t really matter. The call to #require shouldn’t
happen, because the method is supposed to be stubbed out.
You are making a lot of suppositions that are not obvious to us. Nowhere
in your pastie is your require call stubbed.
I’m not sure why the call to #require would need to be stubbed.
What I’m trying to say is that I’ve set a message expectation on
AdSenseHeavenParser#parse , which should prevent the method from being
called in the first place.
The controller method has the require and then calls parse.
Yup! But in my specs, when the controller method calls #parse , the
stub should consume the method call.
One thing which strikes me is that if Rails naming conventions were
being followed:
the file name be lib/ad_sense_heaven_parser not lib/adsense_heaven_parser
Normally, the file would be named like that. However, the class
represents a service called “AdSense Heaven”, which doesn’t have a
space between “Ad” and “Sense”…or should that still cause the file
to be named “ad_sense_…”?
the require wouldn’t be needed since it would be autoloaded.
I thought that everything in lib/ should be autoloaded. When I found
that lib/adsense_heaven_parser.rb wasn’t being autoloaded, I figured I
was wrong, and simply #require’d it in.
Perhaps the problem lies there somewhere.
That’s a good idea. I’ll look into it and let you know.
On Sun, May 24, 2009 at 3:25 PM, Rick DeNatale [email protected]
wrote:
One thing which strikes me is that if Rails naming conventions were
being followed:
the file name be lib/ad_sense_heaven_parser not lib/adsense_heaven_parser
the require wouldn’t be needed since it would be autoloaded.
Perhaps the problem lies there somewhere.
Hi Rick. I finally had some time to fix this problem, and you were
spot on. I moved
lib/adsense_heaven_parser.rb
to
app/models/ad_sense_heaven_parser.rb
and everything’s smooth now.
you can see that the second line is “*** create_keywords_and_associate
***”. However, that string shouldn’t be printed if
AdSenseHeavenParser#parse is stubbed out.
I don’t know why the AdSenseHeavenParser call isn’t being stubbed, but I
see
no reason why that string shouldn’t be printed. You’re not stubbing
create_keywords_and_associate.
I would remove the .with call to see if AdSenseHeavenParser is just
receiving different arguments than you expected.
BTW, class methods are generally indicated with a .; instance methods
with a
#.
///ark
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.