2

在Rails 3.2.13中,默認提供子域。因此,我想測試我的應用程序,以便當商家註冊時,他默認默認爲root_url,子域「商家」即https://merchant.lvh.me:3000。但是,我在RSpec中測試相同的麻煩。我的測試是這樣的:如何訪問rspec集成測試中的rails請求對象?

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: "[email protected]" 
      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 => "lvh.me:3000") 
     end 
       let(:merchant) { User.find_by_email('[email protected]') } 

       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 => "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('[email protected]') } 

       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 

錯誤是:

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: #<NameError: undefined local variable or method `request' for #<Class:0x007fe0d8058848>> 
backtrace: 
/home/app/spec/requests/user_pages_spec.rb:46:in `block (5 levels) in <top (required)>' 
/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 <top (required)>' 
/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 <top (required)>' 
/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 <top (required)>' 
/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 <top (required)>' 
/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 `<top (required)>' 
/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. 

我試圖打印請求對象,檢查是否有是有的,但它的空白。如何在RSpec集成測試中訪問請求對象或者是集成測試是否適合測試這種行爲?請建議。

編輯:更多信息我的數據模型只包含單個用戶表,其中默認情況下都是普通用戶。不過,也有一些商家。現在,當某人以用戶身份登錄(使用登錄表單中的單選按鈕進行區分)時,他應該重定向到user.lvh.me,如果他以商家身份登錄,則應將其重定向到merchant.lvh 。我。這就是我在集成測試中要測試的內容。

回答

2

可以使用

ActionController::TestRequest.new() 

例如

request = ActionController::TestRequest.new(:host => test_domain) 
+0

嗨,我按照你的建議,但仍然得到相同的錯誤。請參閱我已經包含在問題本身中的日誌。 – shailesh 2013-04-23 14:34:02

+0

因爲我沒有看到更多的答案來臨,這是最接近的人去了,我接受它。但是如果有人能夠更好地解釋爲什麼我無法在集成測試中獲得請求,那麼可以接受。 – shailesh 2013-04-25 06:41:40

+0

爲我完美工作,感謝發佈 – 2014-06-13 06:55:31

0

我真不明白你正試圖在這裏測試什麼行爲嘲笑的請求。

看起來你有註冊表單,商家可以註冊。一旦他們註冊,他們應該得到自己的子域(merchant1.lvh.me,merchant2.lvh.me)。是對的嗎?

如果這是正確的,那麼它看起來像你試圖測試應註冊請求的子域。我不認爲它是如何工作的 - 客戶端控制着請求發送給服務器的域。我懷疑你想要做的是在成功註冊適當的小數位後執行重定向。因此,您要測試響應正在重定向到子域。

+0

嗨@Perryn福勒,我已經更新了這個問題。其實有兩類用戶 - 用戶和商家。如果某人以用戶身份登錄,則應將其重定向到user.lvh.me,如果他以商家身份登錄(在登錄表單中使用單選按鈕進行區分),則應將其重定向到merchant.lvh.me 。這就是我在集成測試中要測試的內容。 – shailesh 2013-04-23 14:21:34

相關問題