Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用rspec测试时,应用程序控制器中的请求返回nil_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails 使用rspec测试时,应用程序控制器中的请求返回nil

Ruby on rails 使用rspec测试时,应用程序控制器中的请求返回nil,ruby-on-rails,rspec,Ruby On Rails,Rspec,我用 在我的应用程序控制器中。在开发模式下,它工作,但当我测试它时,它返回 request.subdomain 我有这个测试 undefined method `subdomain' for nil:NilClass 这是应用程序控制器中的方法 describe ApplicationController do controller do def index render text: "hello" end end let(:firm){FactoryGirl

我用

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

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

为什么此测试期间请求方法为nil?

仅在http请求期间初始化请求对象。调用ApplicationController.new.current_公司不是http请求谢谢!这就解决了问题。遇到了一个新问题。我在应用程序控制器中有一个可以向其传递参数的方法。像这样
ftz(时间)
。如何使用
赋值
?赋值用于实例变量(@current_firm)。不是方法。如果你能多解释一下你的场景和相关代码,我可以帮你
def current_firm
   @current_firm ||= Firm.find_by_subdomain!(request.subdomain)
end
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