Rails minitest设计错误

Nik*_*iko 2 testing unit-testing ruby-on-rails minitest devise

我目前正在进行我的第一次测试,但是我遇到了一个我似乎无法解决的问题.我现在已经搜索了几个小时的解决方案并发现了类似的错误,但我没有找到任何解决方案.也许是因为版本差异,我不知道.

当我用minitest编写单元测试时,问题就出现了,也许还有集成测试.我正在使用设计来管理用户.

这是我在终端时出现的错误rake test:

1) Error:
test_deactivate_enrolment(EnrolmentTest):
NoMethodError: undefined method `env' for nil:NilClass
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.0.3/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__840473936918917337__setup__1779200460764756091__callbacks'
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
    /Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:919:in `block in _run_suite'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:912:in `map'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:912:in `_run_suite'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:657:in `block in _run_suites'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:655:in `each'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:655:in `_run_suites'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:867:in `_run_anything'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1060:in `run_tests'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1047:in `block in _run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1046:in `each'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1046:in `_run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1035:in `run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:21:in `run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:774:in `run'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:366:in `block (2 levels) in autorun'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:27:in `run_once'
    /Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:365:in `block in autorun'
Run Code Online (Sandbox Code Playgroud)

这就是我的spec_helper.rb样子:

require 'listen'

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.color_enabled = true
  config.order = :random
  config.filter_run :focus => true
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run_excluding :broken => true
  config.fail_fast = true
end

def test_latency
  0.1
end

# Crash loud in tests!
Thread.abort_on_exception = true
Run Code Online (Sandbox Code Playgroud)

这是我的 test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"

# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"

# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  fixtures :all
  include Devise::TestHelpers

  # Add more helper methods to be used by all tests here...
end
Run Code Online (Sandbox Code Playgroud)

我已经尝试添加include Devise::TestHelpers失败的文件,但没有修复它.删除'test_helper.rb`中的行具有相同的结果.也许有人也有这个错误?

更新

这是测试文件,没有太高级...

要求'test_helper'

class EnrolmentTest < ActiveSupport::TestCase
  test "deactivate enrolment" do
     assert true
  end
end
Run Code Online (Sandbox Code Playgroud)

blo*_*age 20

仅在控制器测试中包含Devise助手.您的test_helper.rb文件应如下所示:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"

# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"

# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end

class ActionController::TestCase
  include Devise::TestHelpers
end
Run Code Online (Sandbox Code Playgroud)