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