[rspec-users] Setting instance variables for controllers

patrick_r patrickdavidreiner at gmail.com
Wed Sep 3 10:22:20 EDT 2008


Hi everyone,

I have been trying to find an answer to this question and have not been able
to. Any help is greatly appreciated.

I am using Rails 2.1.0 and Rspec 1.1.4.

How can I set an instance variable in my Rspec test that is then available
to my controller while the test is running? For example:

(I tried to keep this as simple as possible while illustrating what I mean)


---application_controller.rb---

ApplicationController

  before_filter :set_user

  def set_user
    @user = User.find( 5 )
  end

end


---cart_controller.rb---

CartController

  def index
    if @user
      render :action => 'cart_full'
    else
      render :action => 'cart_empty'
    end
  end

end


---Rspec---

describe CartController do

  describe "index" do
    
    it "should render 'cart_full' if @user is set" do
      # This is where I need to set @user
      response.should render_template( : cart_full )
    end

    it "should render 'cart_empty' if @user is not set" do
      response.should render_template( : cart_empty )
    end

  end

end



Is this even possible to do?

I thought that controller.set_instance_variable( :@user, ... ) would do the
trick but it does not seem to work.

Thank you!
-- 
View this message in context: http://www.nabble.com/Setting-instance-variables-for-controllers-tp19283500p19283500.html
Sent from the rspec-users mailing list archive at Nabble.com.



More information about the rspec-users mailing list