1

I'm trying to test a scenario between several controllers with rspec.

I think "request specs" is the method I should use.

The main problem is that I use the request environment to log in with Basic Authentication.

My controllers tests works well :

describe ClientsController do
  context "With a confirmed user" do
    before(:each) do
      @password = "bidon-bidon"
      @user = ...
    end

    it "should create session" do
      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, @password)
      post :create
      response.should be_success
    end
  end
end

But when I use the method request into my file spec/requests/scenario_spec.rb, it raise an exception:

describe "My scenario" do
  context "With a confirmed user" do
    before(:each) do
      ...
    end

    it "should do several things"
      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, @password)
      post :create
      response.should be_success

      ... some calls to other controllers
    end
  end
end

The request method seems to return nil.

NoMethodError:  undefined method `env' for nil:NilClass

My questions are:

  • Where can I find documentation about request method ?
  • How can I set it ?

I'm using Rails 4 with Rspec 2.14.7.

pierallard
  • 3,326
  • 3
  • 21
  • 48

1 Answers1

4

Taken from this question - Is it possible to specify a user agent in a rails integration test or spec?

Try:

it "should do several things"
  post :create, {}, 'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, @password)
  response.should be_success

  ... some calls to other controllers
end
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93