NameError:未定义的局部变量或方法`app' [英] NameError: undefined local variable or method `app'
本文介绍了NameError:未定义的局部变量或方法`app'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建的控制器规范失败并显示以下错误消息:
The controller spec I created fails with the following error message:
NameError: undefined local variable or method `app' for \
#<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x00000004424880>
...
# spec/controllers/sessions_conroller_spec.rb
require 'spec_helper'
describe SessionsController do
before do
@user = User.gen!
end
describe "#create" do
context "when sending valid email and password" do
it "renders a json hash ..." do
post :create, email: @user.email, password: @user.password
expect(last_response.status).to eq(201)
end
end
end
describe "#destroy" do
context "when sending valid email and authentication token" do
it "renders a json hash ..." do
delete :destroy, email: @user.email, auth_token: @user.authentication_token
expect(last_response.status).to eq(200)
end
end
end
end
spec_helper.rb
加载一些混入.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Rack::Test::Methods, type: :controller
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
推荐答案
我没有像 Jeremy 说的那样配置,仍然有错误
I didn't have this configuration as Jeremy says, and still had the error
我解决了将它添加到 rails_helper.rb 的问题
I solved just adding this to rails_helper.rb
def app
Rails.application
end
但我不确定为什么现在可以工作,知道吗?
But I'm not sure why is working now, any idea?
这篇关于NameError:未定义的局部变量或方法`app'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文