4

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?

mmhan
  • 731
  • 7
  • 18

1 Answers1

0

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(...)

ysbecca
  • 131
  • 5