0

我使用 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

我的问题是:为什么??为什么这个规范失败了?我尝试从浏览器手动测试它,它工作正常。

4

1 回答 1

0

对于那些可能有同样问题的人,不要忘记在测试响应之前做一个请求!!!!!!!即:添加get :new

于 2012-04-26T16:56:24.773 回答