RSpec::ExampleGroups 的未定义方法 define_enum_for

undefined method define_enum_for for RSpec::ExampleGroups

我从 4.1 rails 升级到 4.2.0 后得到 undefined define_enum_for method error

有什么解决办法吗?

Rails : 4.2.0

Ruby : ruby 2.1.5p273(2014-11-13 修订版 48405)[i686-linux]

  1) Recording 
     Failure/Error:
       should define_enum_for(:state).
         with({
           initial: 'initial',
           running: 'running',
           stopped: 'stopped'
         })

     NoMethodError:
       undefined method `define_enum_for' for #<RSpec::ExampleGroups::Recording:0x99a0ea8>

shoulda-matchers#define_enum_for

为 shoulda-matchers#configuration 指定 test_framework :rspec 解决了这个问题。

shoulda-matchers#configuration

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec

    with.library :active_record
    with.library :active_model
    with.library :action_controller
  end
end

需要配置

  1. spec_helper.rb/rails_helper.rb 配置块
  2. 类型:元数据

spec_helper.rb/rails_helper.rb

我需要 spec_helper.rb 中的 Shoulda::Matchers 配置(应用程序尚未迁移到 rails_helper.rb - 但 rails_helper.rb 是我会放置它的地方,如果可用):

# spec/spec_helper.rb
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails # same as :active_record + :active_model + :active_controller
  end
end

元数据

但是,它还需要 type: 元数据才能工作:

This==============\/
describe User, type: :model do
  it { should define_enum_for(:status) }
end