关于单元测试:Rails minitest设计错误

Rails minitest devise error

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

当我使用minitest编写单元测试时,可能会出现问题,也许还有集成测试。我正在使用devise来管理用户。

当我rake test

时,在终端中出现此错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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'

这是我的spec_helper.rb的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

这是我的test_helper.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

我尝试在失败的文件之上添加include Devise::TestHelpers,但这并不能解决问题。删除" test_helper.rb"中的行具有相同的结果。也许有人也有此错误?

更新

这是测试文件中的一个,没有什么太高级的了...

需要'test_helper'

1
2
3
4
5
class EnrolmentTest < ActiveSupport::TestCase
  test"deactivate enrolment" do
     assert true
  end
end


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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


或者,您可以使用Warden来代替Device test helpers进行测试,这里有一个使用示例。设计基于Warden。


从test_helper.rb中删除include Devise::TestHelpers。仅将其包括在需要它的测试中。