Weirdness with rake spec!

Hi everyone.

I am totally confused by this… If I run rake spec, I get a bunch of
failures
in a particular file, but if I run script/spec on that individual file,
all
examples pass.

The error I am getting is with the pluralize method. I put a debugger
statement in my code to inspect what is happening:

(rdb:1) pluralize(5, ‘foo’)
ArgumentError Exception: wrong number of arguments (2 for 0)

This is a situation where I am doing the “self-shunt” method, because I
don’t
know of a better way to accomplish this. In other words, I am doing:

class Foo

def initialize(template, number)
@template = template
@number = number
end

def bar
pluralize(@number, ‘foo’)
end

def bar_link
link_to bar, foo_bar_path
end

def method_missing(*args, &block)
@template.send(*args, &block)
end

end

Then in my spec I do:

Foo.new(self, 2).bar.should == “foos”

And like I said, if I run this spec individually, it passes, but with
rake
spec, it gives me that wrong number of arguments error.

I would love to structure this differently so that I don’t need to pass
self
into the class initializer, but I am at a total loss for how to do that.

If I do:

include ActionView::Helpers
include ActionController::UrlWriter

Then I get “can’t convert string into hash” errors when ever I try to
access a
named route with link_to.

This is a problem I have had to face over and over, and has proven to be
quite
frustrating. The only solution I have found is to pass self into the
method…
So if anyone has any other suggestions, I’d love to hear it.

Thank you.

Patrick J. Collins
http://collinatorstudios.com

On Jun 23, 2010, at 12:07 PM, Patrick J. Collins wrote:

ArgumentError Exception: wrong number of arguments (2 for 0)
@number = number
def method_missing(*args, &block)

include ActionView::Helpers
include ActionController::UrlWriter

Then I get “can’t convert string into hash” errors when ever I try to access a
named route with link_to.

This is a problem I have had to face over and over, and has proven to be quite
frustrating. The only solution I have found is to pass self into the method…
So if anyone has any other suggestions, I’d love to hear it.

Is this a rails helper module? Is the spec in spec/helpers?

Is this a rails helper module? Is the spec in spec/helpers?

Well it’s not a module-- as in it’s not getting mixed into any other
class.
It’s just being instantiated from helpers (and inside the spec)… But
since it utilizes helper methods, I did put it in the helpers folder–
It’s
actually in:

spec/helpers/renderers

Patrick J. Collins
http://collinatorstudios.com

Why not just use a rails helper? Then you get all the other helpers and environment for free.

Well, these are usually complex helpers that build a lot of html, and I
utilize
a lot of instance variables to share between methods… My practice has
been
to put these things in their own class so that I won’t have conflicts
with
other instance variables…

I don’t understand though why rake spec would fail but script/spec
wouldn’t…
Is there something different about the environment when rake spec is
called?

Patrick J. Collins
http://collinatorstudios.com

On Jun 23, 2010, at 3:25 PM, Patrick J. Collins wrote:

Why not just use a rails helper? Then you get all the other helpers and environment for free.

Well, these are usually complex helpers that build a lot of html, and I utilize
a lot of instance variables to share between methods… My practice has been
to put these things in their own class so that I won’t have conflicts with
other instance variables…

I’d recommend slinging values around rather than storing them in
instance variables. Passing values around makes each method much easier
to test, and you don’t have to concern yourself with values persisting
across requests, etc, etc.

I don’t understand though why rake spec would fail but script/spec wouldn’t…
Is there something different about the environment when rake spec is called?

There can be. It depends on what else is going on in your app. For one
thing, running rake loads all of the .rake files in the lib/tasks
directories of your app and all installed plugins.

Could also be a load order thing. The fact that you sometimes get an
argument error on pluralize suggests that there is more than one
definition of pluralize in your app or one of its dependencies, and
they’re not getting loaded in the same order with both commands.

Not sure if that helps solve your problem, but hopefully it sheds some
light on it.

Cheers,
David

On Jun 23, 2010, at 1:38 PM, Patrick J. Collins wrote:

Is this a rails helper module? Is the spec in spec/helpers?

Well it’s not a module-- as in it’s not getting mixed into any other class.
It’s just being instantiated from helpers (and inside the spec)… But
since it utilizes helper methods, I did put it in the helpers folder-- It’s
actually in:

spec/helpers/renderers

Why not just use a rails helper? Then you get all the other helpers and
environment for free.