簡體   English   中英

NameError:未定義的局部變量或方法`app'

[英]NameError: undefined local variable or method `app'

我創建的控制器規范失敗,並顯示以下錯誤消息:

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

spec_helper.rb刪除以下spec_helper.rb

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

我沒有像Jeremy所說的這種配置,但仍然有錯誤

我解決了只是將它添加到rails_helper.rb

  def app
    Rails.application
  end

但我不知道為什么現在有效,有什么想法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM