#<RSpec::Core::Configuration:0x007fccf515e338> 的未定义方法 `use_transactional_tests=' (NoMethodError)

undefined method `use_transactional_tests=' for #<RSpec::Core::Configuration:0x007fccf515e338> (NoMethodError)

我正在测试新的 Rails 版本 5.0.0.alpha 和 RSpec 3.1.7 但我收到错误消息:

undefined method `use_transactional_tests=' for #<RSpec::Core::Configuration:0x007fccf515e338> (NoMethodError)

rails_helper.rb 我经常使用这个并且一切正常:

config.use_transactional_fixtures = true

但是我收到了这个警告:

DEPRECATION WARNING: use_transactional_fixtures= is deprecated and will be removed from Rails 5.0 (use use_transactional_tests= instead). (called from block in <module:FixtureSupport> at /Users/monteirobrena/.rvm/gems/ruby-2.2.2@global/gems/rspec-rails-3.1.0/lib/rspec/rails/fixture_support.rb:25)

所以,我更改了 rails_helper.rb 以使用新方法:

config.use_transactional_tests = true

现在我收到了这个错误:

/Users/monteirobrena/project/spec/rails_helper.rb:34:in `block in <top (required)>': undefined method `use_transactional_tests=' for #<RSpec::Core::Configuration:0x007fccf515e338> (NoMethodError)
from /Users/monteirobrena/.rvm/gems/ruby-2.2.2@global/gems/rspec-core-3.1.7/lib/rspec/core.rb:81:in `configure'
from /Users/monteirobrena/project/spec/rails_helper.rb:27:in `<top (required)>'
from /Users/monteirobrena/project/spec/controllers/messages_controller_spec.rb:1:in `require'
from /Users/monteirobrena/project/spec/controllers/messages_controller_spec.rb:1:in `<top (required)>'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /Users/monteirobrena/.rvm/gems/ruby-2.2.2@global/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/bin/rspec:23:in `load'
from /Users/monteirobrena/.rvm/rubies/ruby-2.2.2/bin/rspec:23:in `<main>'
from /Users/monteirobrena/.rvm/gems/ruby-2.2.2@global/bin/ruby_executable_hooks:15:in `eval'
from /Users/monteirobrena/.rvm/gems/ruby-2.2.2@global/bin/ruby_executable_hooks:15:in `<main>'

有什么想法吗?谢谢。

您收到的弃用警告和建议来自 Rails,而不是 RSpec 或 rspec-rails gem。 rspec-rails gem 实现了 use_transactional_fixtures= and turns around and calls the same method in Rails。它没有实现 use_transactional_tests=,因此您在进行更改时会收到该错误消息。

除非 rspec-rails gem 升级支持新的 Rails 5 功能,否则您需要设置 Rails 配置直接 - 从 rspec-rails 3.8 开始,它不会。

您需要继续使用 config.use_transactional_fixtures = true in rspec 配置 rails 5 beta(至少在正式发布之前,不确定 rspec 团队计划支持这个)。 修复已经在 master 分支上,here is the line fixing it.

在 master 分支上设置 rspec 个 gem:

%w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib|
    gem lib, git: "https://github.com/rspec/#{lib}.git", branch: 'master'
end

我更新了我的 rspec 版本,所以我不需要更改我的 use_transactional_fixtures 行。与此同时,只有一个测试版:

gem 'rspec', '~> 3.5.0.beta4'
gem 'rspec-rails', '~> 3.5.0.beta4'