Rspec-rails: Can no longer set host! in before block for request specs

Created on 9 Jul 2016  路  9Comments  路  Source: rspec/rspec-rails

I just upgraded to Rails 5 and Rspec 3.5 All my tests are passing except request specs. My request specs are testing API interactions. The API is scoped to the subdomain api. I have a before block that sets the host!. I see the block is executing and host is setting but when the actual test is executing the host change reverts to www.example.com and I get a routing error.

Within this block host is set and working.

Rspec.configure do |config|
  config.before(:all, type: :request) do
    host! 'api.lvh.me'
  end
end

The test, host is no longer set. If I set it manually in this code the test will pass:

describe 'GET /api/v1/boxes', type: :request do
  it 'returns a list of boxes' do
    account = create(:account)

    get '/v1/boxes'

    expect(JSON.parse(response.body)['boxes'].count).to eq(account.boxes.count)
  end
end

What am I doing wrong here?

Has reproduction case

Most helpful comment

@wassimk

I have opened a pull request on rails to fix this, see https://github.com/rails/rails/pull/26235.

In the mean time, setting host! in a before(:each) block should fix the problem for you. I'll close this when rails merges it since we have a work around.

All 9 comments

Whats defining host! and how is it persisting it across examples? I'm wondering if we've inadvertently changed something that was relying on.

@JonRowe host! is defined in ActionDispatch::Integration::RequestHelpers::Session in Rails https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/integration.rb#L287.

Are you resetting the session anywhere? We did ship a load ordering change in 3.5 that shouldn't have been breaking but might be in your scenario, as if the session is reset between your hook and your spec it would exhibit this behaviour...

I have a feeling that I know what this is, some of the ways we interface with ActionDispatch::TestRequest were changed in 3.4 -> 3.5 to support rails 5. @mrageh is it easy for you to give us a sample rails app which shows the issue? Here are the usual steps I ask people to follow:

Could you please provide us with a rails app that we can clone that demonstrates the issue. Specifically it'd be great if

1) you could rails new an application and commit
2) make all the changes necessary to reproduce the issue and commit

then, provide us with a description of how to clone your application and reproduce the issue.

Thanks :)

Hi @samphippen , I have a repo at https://github.com/wassimk/rspec-rails-1665-issue and I made a short screencast walking through what I'm experiencing at http://www.screencast.com/t/nyZ2jKQi

Thanks for your help!

fa63448420d3385dbd043aca22dba973b45b8bb2 is the commit a git bisect points to in rails.

@wassimk

I have opened a pull request on rails to fix this, see https://github.com/rails/rails/pull/26235.

In the mean time, setting host! in a before(:each) block should fix the problem for you. I'll close this when rails merges it since we have a work around.

@samphippen since you've fixed the underlying problem causing this issue in Rails, do you think we should close this issue?

Yeah. This can be closed now.

Was this page helpful?
0 / 5 - 0 ratings