【问题标题】:NameError: undefined local variable or method `app'NameError:未定义的局部变量或方法“app”
【发布时间】:2013-08-19 03:44:30
【问题描述】:

我创建的控制器规范失败并显示以下错误消息:

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

【问题讨论】:

标签: ruby-on-rails rspec rack rspec-rails


【解决方案1】:

我没有 Jeremy 所说的这个配置,但仍然有错误

我解决了只是将它添加到 rails_helper.rb

  def app
    Rails.application
  end

但我不确定为什么现在可以工作,知道吗?

【讨论】:

    【解决方案2】:

    从您的 spec_helper.rb 中删除以下内容

    config.include Rack::Test::Methods, type: :controller
    

    【讨论】:

    • 对于其他任何人都将头撞到墙上,得到“NameError:未定义的局部变量或方法‘app’”错误。当您一次运行所有测试(多个文件)并且其中一个执行include Rack::Test::Methods 时,也会发生这种情况 - 包含“感染”其他测试。所以症状是当文件单独运行时所有测试都通过了,但是当它们一起运行时它们会失败并出现“无应用程序”错误。至少 rails 3.0.9rspec 3.0 会发生这种情况
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多