title: How to access rails request object in rspec integration test?
body: <p>In Rails 3.2.13, subdomain is provided by default. As such, I want to test my application such that when a merchant signs up, he goes by default to root_url with subdomain 'merchant' i.e. <a href="https://merchant.lvh.me:3000" rel="nofollow">https://merchant.lvh.me:3000</a>. However, I am having trouble testing the same in RSpec. My test looks like this:</p>
  
  <pre><code>  describe "Sign in" do
      before { visit signup_path }
      let(:submit) { "Sign up" }
  
      describe "with invalid information" do
          it "should not create a user" do
              expect { click_button submit }.not_to change(User, :count)
          end
      end
  
      describe "with valid information" do
          before do
              fill_in "Email", with: "user@gmail.com"
              fill_in "Password", with: "securepassword"
          end
  
          describe "as a merchant" do
              before { choose("Merchant") }
  
              it "should create a merchant user" do
                  expect { click_button submit }.to change(User, :count).by(1)
              end
  
              describe "after creating the merchant user" do
                  before do
              click_button submit
              request = ActionController::TestRequest.new(:host =&gt; "lvh.me:3000")
           end
                  let(:merchant) { User.find_by_email('user@gmail.com') }
  
                  expect (request.subdomain).to eq('merchant')
                  it { should have_selector 'div.alert.alert-success', text: "Welcome to App!" }
                  it { should have_link "Sign out", href: signout_path }
                  it { should_not have_link "Sign in", href: signin_path }
                  it { should have_content merchant.email }
            it { should have_selector "title", text: full_title(merchant.email) }
              end
          end
  
          describe "as a user" do
              before do
            choose("User")
            request = ActionController::TestRequest.new(:host =&gt; "lvh.me:3000")
          end
  
              it "should create a normal user" do
                  expect { click_button submit }.to change(User, :count).by(1)
              end
  
              describe "after creating the normal user" do
                  before { click_button submit }
                  let(:user) { User.find_by_email('user@gmail.com') }
  
                  expect (request.subdomain).to eq('user')
                  it { should have_selector 'div.alert.alert-success', text: "Welcome to App!" }
                  it { should have_link "Sign out", href: signout_path }
                  it { should_not have_link "Sign in", href: signin_path }
            it { should have_content user.email }
            it { should have_selector "title", text: full_title(user.email) }
              end
          end
      end
    end
  </code></pre>
  
  <p>Error is:</p>
  
  <pre><code>19:58:54 - INFO - Guard::RSpec is running, with RSpec 2!
  19:58:54 - INFO - Running all specs
  Running tests with args ["--drb", "-f", "progress", "-r", "/usr/local/lib/ruby/gems/1.9.1/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
  Exception encountered: #&lt;NameError: undefined local variable or method `request' for #&lt;Class:0x007fe0d8058848&gt;&gt;
  backtrace:
  /home/app/spec/requests/user_pages_spec.rb:46:in `block (5 levels) in &lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
  /home/app/spec/requests/user_pages_spec.rb:39:in `block (4 levels) in &lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
  /home/app/spec/requests/user_pages_spec.rb:32:in `block (3 levels) in &lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
  /home/app/spec/requests/user_pages_spec.rb:26:in `block (2 levels) in &lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
  /home/app/spec/requests/user_pages_spec.rb:16:in `block in &lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
  /home/app/spec/requests/user_pages_spec.rb:3:in `&lt;top (required)&gt;'
  /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
  /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
  /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
  /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
  /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in `run_tests'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:13:in `block in run'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:21:in `block in initialize'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `fork'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/forker.rb:18:in `initialize'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `new'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/run_strategy/forking.rb:9:in `run'
  /usr/local/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/server.rb:48:in `run'
  /usr/local/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
  /usr/local/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
  /usr/local/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
  /usr/local/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
  /usr/local/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
  Done.
  </code></pre>
  
  <p>I tried to print request object to see if anything is there, but its blank. How to access request object in RSpec integration test or is integration test the right place to test for this kind of behavior? Please suggest.</p>
  
  <p><strong>Edit: More Information</strong> My data model consists of only single user table where all by default are normal users. However, some are merchants also. Now, when someone signs-in as a user (distinguished using radio button in sign-in form), he should be redirected to user.lvh.me and if he signs-in as a merchant, he should be redirected to merchant.lvh.me. This is what I am trying to test in my integration tests.</p>