Nick Hoffman
2008-Dec-04 20:41 UTC
[rspec-users] NameError when passing a URL helper to a method
Hi guys. I just wrote a small spec helper method to DRY up some of my specs, and found that passing a URL helper (Eg: #photos_url) to a method results in a NameError error. I extracted the problem into its own short spec file to isolate it. Any thoughts on what''s going on?: http://gist.github.com/32060 Thanks for your help, Nick
Zach Dennis
2008-Dec-04 22:06 UTC
[rspec-users] NameError when passing a URL helper to a method
On Thu, Dec 4, 2008 at 3:41 PM, Nick Hoffman <nick at deadorange.com> wrote:> Hi guys. I just wrote a small spec helper method to DRY up some of my specs, > and found that passing a URL helper (Eg: #photos_url) to a method results in > a NameError error. I extracted the problem into its own short spec file to > isolate it. Any thoughts on what''s going on?: > http://gist.github.com/32060foo(properties_url) is being executed in the context of the describe block and not in the instance of an example being run. For example: describe FooController do it "has access to routing methods here" do properties_url end # it doesn''t have access out here though, this fails properties_url # your example will fail to foo(properties_url) end -- Zach Dennis http://www.continuousthinking.com http://www.mutuallyhuman.com
Nick Hoffman
2008-Dec-04 22:43 UTC
[rspec-users] NameError when passing a URL helper to a method
On 2008-12-04, at 17:06, Zach Dennis wrote:> On Thu, Dec 4, 2008 at 3:41 PM, Nick Hoffman <nick at deadorange.com> > wrote: >> Hi guys. I just wrote a small spec helper method to DRY up some of >> my specs, >> and found that passing a URL helper (Eg: #photos_url) to a method >> results in >> a NameError error. I extracted the problem into its own short spec >> file to >> isolate it. Any thoughts on what''s going on?: >> http://gist.github.com/32060 > > foo(properties_url) is being executed in the context of the describe > block and not in the instance of an example being run. For example: > > describe FooController do > it "has access to routing methods here" do > properties_url > end > > # it doesn''t have access out here though, this fails > properties_url > > # your example will fail to > foo(properties_url) > end > -- > Zach DennisHi Zach. Thanks for that explanation; it makes a lot of sense. What I''m trying to do is write a spec helper method for DRYing up controller redirect examples. In other words, rather than having the following 4 lines repeated throughout my specs: it ''should redirect to the account page'' do do_action response.should redirect_to account_url end I''d like to do this: it_should_redirect_to ''the account page'', account_url I''ve written the method. The only problem is what you explained; #account_url being called from outside of an example. The only solution that I can think of is to do this: before :each do @account_url = account_url end it_should_redirect_to ''the account page'', @account_url Are there any alternative solutions? Cheers, Nick
Nick Hoffman
2008-Dec-04 22:49 UTC
[rspec-users] NameError when passing a URL helper to a method
On 2008-12-04, at 17:43, Nick Hoffman wrote:> The only solution that I can think of is to do this: > before :each do > @account_url = account_url > end > it_should_redirect_to ''the account page'', @account_urlActually, that suggestion above of mine doesn''t work. It fails with: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.rewrite (eval):17:in `account_url'' I thought of it while writing that last email, but didn''t test it before hitting send. -Nick
Pat Maddox
2008-Dec-04 22:54 UTC
[rspec-users] NameError when passing a URL helper to a method
Nick Hoffman <nick at deadorange.com> writes:> Hi guys. I just wrote a small spec helper method to DRY up some of my > specs, and found that passing a URL helper (Eg: #photos_url) to a > method results in a NameError error. I extracted the problem into its > own short spec file to isolate it. Any thoughts on what''s going on?: > http://gist.github.com/32060 > > Thanks for your help, > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersCheck out my attempt: http://gist.github.com/32130 I don''t know which one is correct. Pat
Pat Maddox
2008-Dec-05 00:56 UTC
[rspec-users] NameError when passing a URL helper to a method
Nick Hoffman <nick at deadorange.com> writes:> On 2008-12-04, at 17:43, Nick Hoffman wrote: >> The only solution that I can think of is to do this: >> before :each do >> @account_url = account_url >> end >> it_should_redirect_to ''the account page'', @account_url > > Actually, that suggestion above of mine doesn''t work. It fails with: > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.rewrite > (eval):17:in `account_url'' > > I thought of it while writing that last email, but didn''t test it > before hitting send. > -Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersIt''s the same as class Foo @class_instance_variable = "blah" def foo puts @class_instance_variable end end Foo.foo will print nothing, because @class_instance_variable is nil in the object instance. Anyway, I thought of an even cleaner way to do things than using a block, assuming you only care about calling one method: do_something(:blah_url_helper) def do_something(helper_name) send(helper_name) end Pat
Mark Wilden
2008-Dec-05 01:27 UTC
[rspec-users] NameError when passing a URL helper to a method
On Thu, Dec 4, 2008 at 4:56 PM, Pat Maddox <pergesu at gmail.com> wrote:> class Foo > @class_instance_variable = "blah" > > def foo > puts @class_instance_variable > end > end > > Foo.foo will print nothing, because @class_instance_variable is nil in > the object instance. >I don''t know if it matters here, but you can define a class method to return a class instance variable. ///ark -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081204/e156cebf/attachment.html>
Nick Hoffman
2008-Dec-05 18:06 UTC
[rspec-users] NameError when passing a URL helper to a method
On 2008-12-04, at 19:56, Pat Maddox wrote:> Nick Hoffman <nick at deadorange.com> writes: >> On 2008-12-04, at 17:43, Nick Hoffman wrote: >>> The only solution that I can think of is to do this: >>> before :each do >>> @account_url = account_url >>> end >>> it_should_redirect_to ''the account page'', @account_url >> >> Actually, that suggestion above of mine doesn''t work. It fails with: >> You have a nil object when you didn''t expect it! >> The error occurred while evaluating nil.rewrite >> (eval):17:in `account_url'' >> >> I thought of it while writing that last email, but didn''t test it >> before hitting send. >> -Nick >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > It''s the same as > > class Foo > @class_instance_variable = "blah" > > def foo > puts @class_instance_variable > end > end > > Foo.foo will print nothing, because @class_instance_variable is nil in > the object instance. > > Anyway, I thought of an even cleaner way to do things than using a > block, assuming you only care about calling one method: > > do_something(:blah_url_helper) > > def do_something(helper_name) > send(helper_name) > end > > PatHi Pat. That''s a great suggestion. Here''s what I ended up doing: http://gist.github.com/32424 Thanks, Nick