我使用ActsAsTenant将多租户应用于我的应用程序。我通过检查当前租户是否为nil
,在每次请求之前检查子域。如果当前租户为nil
,我会将用户重定向到404页面:
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
我的问题是:为什么?为什么这个规范失败了?我尝试从浏览器手动测试它,它运行正常。
答案 0 :(得分:0)
对于那些可能遇到同样问题的人,在测试响应之前不要忘记做一个请求!!!!!即:添加get :new