2012-04-26 34 views
0

我使用ActsAsTenant將多租戶應用於我的應用程序。我在每次請求之前檢查子域是否通過檢查當前租戶是否爲nil。如果目前的租戶是nil,我將用戶重定向到404頁:RSpec:驗證當前子域

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    set_current_tenant_by_subdomain(:client, :account_name) 

    before_filter :check_subdomain 

    private 

    def check_subdomain 
    redirect_to("/404.html") if ActsAsTenant.current_tenant.nil? 
    end 
end 

我有以下規範來測試此行爲:

require 'spec_helper' 

describe SessionsController do 

    let(:client) { create(:client) } 

    before(:each) do 
    @request.host = "#{client.account_name}.lvh.me" 
    end 

    describe "GET 'new'" do 
    context "for invalid subdomains" do 
     it "should redirect the user to the 404 page" do 
     @request.host = "foo.lvh.me" 
     response.should redirect_to "/404.html" 
     end 
    end 
    end 
end 

該規範失敗,出現以下錯誤信息:

F 

Failures: 

    1) SessionsController GET 'new' for invalid subdomains should redirect the user to the 404 page 
    Failure/Error: response.should redirect_to "/404.html" 
     Expected response to be a <:redirect>, but was <200> 
    # ./spec/controllers/sessions_controller_spec.rb:15:in `block (4 levels) in <top (required)>' 

Finished in 0.2098 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/controllers/sessions_controller_spec.rb:13 # SessionsController GET 'new' for invalid subdomains should redirect the user to the 404 page 

我的問題是:爲什麼?爲什麼此規範失敗?我試圖從瀏覽器手動測試它,它工作正常。

+1

順便說一句,絕對真棒問題格式!我希望更多的人提出這樣的問題。它的SICCO!具體的,內容豐富的,簡潔的和話題性的!對你好! – ddd 2012-06-15 16:59:28

回答

0

對於那些可能有同樣問題的人,不要忘記在測試響應之前做出請求!!!!!即:添加get :new