1
我想測試我的控制器posts
當我運行Rspec
測試,殼告訴我一個錯誤NoMethodError:未定義的方法`ENV」的零:NilClass
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /home/weare138/.rvm/gems/ruby-2.1.5/gems/devise-3.4.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
這是我spec/requests/posts_spec.rb
require 'rails_helper'
describe "Posts" do
describe "GET /posts" do
it "works! (now write some real specs)" do
user = FactoryGirl.create(:user, role: 'admin')
sign_in user
end
end
end
UPD
rails_helper
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::TestHelpers, :type => :request
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
怎麼修?
謝謝你,我發現這個問題,但我不明白這個desicion – vveare138
你是否在你的測試環境中添加了這些助手,就像它在readme中指定的那樣https://github.com/plataformatec/devise#test-幫手? – rastasheep
是的,我添加了。更新後的問題 – vveare138