Forum: RSpec undefined local variable or method `params' for #<Spec

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Salil G. (Guest)
on 2009-04-14 10:24
following is the method of the helper

module ApplicationHelper
  def tab_class(tab)
    'class="active"' if tab == params[:controller]
  end
end

i write the spec method as follows

describe ApplicationHelper do
   it "should be active if controller is same" do
    tab_class('royalty_statement').should include('active')
   end
end

it gives me following error

undefined local variable or method `params' for
#<Spec::Rails::Example::RailsExa
mpleGroup::Subclass_1:0x5234150>

Regards

Salil
Aslak H. (Guest)
on 2009-04-14 10:57
(Received via mailing list)
On Tue, Apr 14, 2009 at 8:24 AM, Salil G. <removed_email_address@domain.invalid>
wrote:

> describe ApplicationHelper do
>
We need the full trace to analyse this

Aslak
Fernando P. (Guest)
on 2009-04-14 16:01
> it gives me following error
>
> undefined local variable or method `params' for

That's normal. Remember that params exists because there is an http
request issued. In your spec, you don't tell Rspec anything about any
request. So you could create a mock for params. If I recall correctly
you would have to do something like this:

describe ApplicationHelper do
   it "should be active if controller is same" do
    controller.params = {:controller => 'whatever_value'}
    tab_class('royalty_statement').should include('active')
   end
end
Salil G. (Guest)
on 2009-04-14 16:23
Fernando P. wrote:
>> it gives me following error
>>
>> undefined local variable or method `params' for
>
> That's normal. Remember that params exists because there is an http
> request issued. In your spec, you don't tell Rspec anything about any
> request. So you could create a mock for params. If I recall correctly
> you would have to do something like this:

Thanx fernando
i try the following

it "should be active if controller is same" do
    params = {:controller => 'royalty_statement'}
    tab_class('royalty_statement').should include('active')
  end

but it doesn't work out. it gives me same error as previus.
Fernando P. (Guest)
on 2009-04-14 17:01
> it "should be active if controller is same" do
>     params = {:controller => 'royalty_statement'}
>     tab_class('royalty_statement').should include('active')
>   end
>
> but it doesn't work out. it gives me same error as previous.

Nah it doesn't work that way, because remember that RSpec is just Ruby
code. 'it' is just a method, "should be active ..." is just an argument,
and then you specify a block of code.

So when you define params = ..., you are actually defining a local
variable, but that variable is not available inside the controller when
the spec is run. That's why I find controllers so painful to spec. I
prefer to use cucumber and webrat for that purpose.

Anyway, I thought that setting controller.params would do the trick but
no.

Maybe you'll have to force a get request then?  so it would be:
get :action_name

We need to wait for rspec-rails experts advice.

By the way the same problem applies to session, so you might be luckier
looking for info about simulating session in rspec and then apply the
same technique to params.
Fernando P. (Guest)
on 2009-04-14 17:04
Try with that:

controller.stub!(:params).and_return({:controller =>
'your_controller_name'})
Salil G. (Guest)
on 2009-04-14 18:00
> Try with that:
>
> controller.stub!(:params).and_return({:controller =>
> 'your_controller_name'})

Nah it doesn't work out,gives same error.
neways thanx for the info.
Chris F. (Guest)
on 2009-04-14 18:12
(Received via mailing list)
helper.stub! .... the OP is testing a helper method.
Salil G. (Guest)
on 2009-06-08 09:21
Hi All,

Any Update on this topic.

I still not found any solution for a following method.

 it "should be active if controller is same" do
    tab_class('tab').should include('active')
  end


Regards,

Salil
David C. (Guest)
on 2009-06-08 16:57
(Received via mailing list)
On Mon, Jun 8, 2009 at 12:21 AM, Salil G.<removed_email_address@domain.invalid>
wrote:
> Hi All,
>
> Any Update on this topic.
>
> I still not found any solution for a following method.
>
>  it "should be active if controller is same" do
>    tab_class('tab').should include('active')
>  end

Please read http://wiki.github.com/dchelimsky/rspec/get-in-touch and
try again. There is a lot of missing context information here.

Thanks,
David
This topic is locked and can not be replied to.