ruby-on-rails-3rspecrspec-railsfacebooker2

How do I rspec with valid_session using Facebooker2 in rails 3?


I'm pretty new to Rails. I have been developing my app with RSpec. I have just followed a tutorial for Facebooker2 and added the before_filter for authentication.

#app/controller/application_controller.rb
before_filter :ensure_authenticated

However, my specs such as the one below failed because there's no valid session which is understandable.

#spec/controllers/answers_controller_spec.rb
describe "GET index" do
  it "assigns all answers as @answers" do
    get :index, {}, valid_session
    assigns(:answers).should include(answers(:reading))
  end
end

But I can't seem to find a way to fake a valid session for rspec to use in the following function.

#spec/controllers/answers_controller_spec.rb
def valid_session
  {}
end

Is there a way at all? If not, what is the rails way of doing things for such a case?


Solution

  • If you aren't concerned about testing the actual authentication and just want to simulate a valid session, you could stub out the filter:

    controller.stub(:ensure_authenticated).and_return(true)
    

    EDIT: you may need ApplicationController.stub(...)