3
我創建的控制器規範失敗,出現以下錯誤信息:NameError:未定義的局部變量或方法'應用」
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
這可能是有用的:https://www.ruby-forum.com/topic/1068659 – vee
然後我不再有權訪問['last_response'](https://github.com/brynary/rack-test /blob/v0.6.2/lib/rack/mock_session.rb#L55)。但你是對的:當我使用'response'代替**時,**第一次測試通過了**。然而,「破壞」行爲測試**失敗**:「預期:200得到:401」,但這可能與**身份驗證令牌**有關。我想知道'app' **可以如何定義** .. – JJD
像這樣的東西應該解決它嗎? https://gist.github.com/alex-zige/5795358(自定義Rspec助手Rake :: Test :: API範圍的方法。) – vee