3

我用

request.subdomain 

在我的应用程序控制器中。在开发模式下它可以工作,但是当我测试它时它会返回

undefined method `subdomain' for nil:NilClass

我有这个测试。

describe ApplicationController do
controller do
  def index
    render text: "hello"
  end   
    end
let(:firm){FactoryGirl.create(:firm, subdomain: "test")}
let(:user){FactoryGirl.create(:user, firm: firm)}
describe "current" do
  before(:each) do 
    @request.host = "#{firm.subdomain}.example.com"
      sign_in(user) 
      get :index, subdomain: "test"
  end
  it "should current_firm" do
     ApplicationController.new.current_firm.subdomain.should == "test"
  end
    end
end

这是应用程序控制器中的方法

def current_firm
   @current_firm ||= Firm.find_by_subdomain!(request.subdomain)
end

为什么在这个测试期间请求方法为零?

4

1 回答 1

2
it "should current_firm" do
   @request.host = "#{firm.subdomain}.example.com"
   sign_in(user) 
   get :index, subdomain: "test"
   assigns(:current_firm).subdomain.should == "test"
end
于 2013-06-03T13:38:38.817 回答