Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a variable in my rspec test so that it can be used by the controller for a query?

I have a variable in my sessions controller.

session[:facebook_profile_id] = @user_info['id']

@user_info['id'] is an int. Example: 123

I then use that session variable in my main controller to get a profile object from the database.

def show
    @facebook_profile = FacebookProfile.find_by_facebook_id(session[:facebook_profile_id])
end

The new object is found using the session variable and is used in my application, so my rspec test fails without it.

Here is my Factory for FacebookProfile:

FactoryGirl.define do 
  factory :facebook_profile do |f|
    f.facebook_id 123
  end
end

In my spec test for the application, I create the Factory instance before each test:

FactoryGirl.create(:facebook_profile).should be_valid

How do I set the session[:facebook_profile_id] variable in my spec test so that the lookup for @facebook_profile doesn't fail?

I've tried stubbing, but couldn't get it to work. Also, I've been trying this in my features spec. Should I be doing this in the controller spec?

like image 568
dblarons Avatar asked Jan 13 '23 14:01

dblarons


1 Answers

If your purpose is functional testing, it's simple to just assign it directly in test

# describe .....
  session[:facebook_profile_id] = 123
# end

After assignment you can get the variable directly as well

session[:facebook_profile_id] 
like image 179
Billy Chan Avatar answered Jan 17 '23 14:01

Billy Chan