Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can no longer set host! in before block for request specs #1662

Closed
wassimk opened this issue Jul 9, 2016 · 9 comments
Closed

Can no longer set host! in before block for request specs #1662

wassimk opened this issue Jul 9, 2016 · 9 comments
Assignees

Comments

@wassimk
Copy link

wassimk commented Jul 9, 2016

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?

@JonRowe
Copy link
Member

JonRowe commented Jul 10, 2016

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

@mrageh
Copy link

mrageh commented Jul 10, 2016

@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.

@JonRowe
Copy link
Member

JonRowe commented Jul 11, 2016

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

@fables-tales
Copy link
Member

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 :)

@wassimk
Copy link
Author

wassimk commented Jul 12, 2016

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!

@fables-tales
Copy link
Member

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

fables-tales pushed a commit to fables-tales/rails that referenced this issue Aug 20, 2016
…egration::Runner.

In commit fa63448, @tenderlove changed
the behaviour of the way `integration_session` is set up in this object.
It used to be the case that the first time it was accessed, it was
memoized with nil, however, this means that if it had already been set
it was not replaced. After that commit, it is now always set to `nil` in
the execution of `before_setup`.

In RSpec, users are able to invoke `host!` in `before(:all)` blocks,
which execute well before `before_setup` is ever invoked (which happens
in what is equivalent to a `before(:each)` block, for each test. `host!`
causes the integration session to be set up to correctly change the
host, but after fa63448 the
`integration_session` gets overwritten, meaning that users lose their
`host!` configuration (see rspec/rspec-rails#1662).

This commit changes the behaviour back to memoizing with `nil`, as
opposed to directly overwriting with `nil`. This causes the correct
behaviour to occur in RSpec, and unless I'm mistaken will also ensure
that users who want to modify their integration sessions early in rails
will also be able to do so.
@fables-tales
Copy link
Member

@wassimk

I have opened a pull request on rails to fix this, see rails/rails#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.

@mrageh
Copy link

mrageh commented Aug 25, 2016

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

@fables-tales
Copy link
Member

Yeah. This can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants