【问题标题】:RSpec tests failing with undefined method `validate_uniqueness_of'RSpec 测试失败,未定义方法“validate_uniqueness_of”
【发布时间】:2017-03-01 23:58:31
【问题描述】:

使用 Rails 5 和 RSpec 3.5,我收到以下错误。

  1) User
     Failure/Error: it { should validate_uniqueness_of(:auth_token)}

     NoMethodError:
       undefined method `validate_uniqueness_of' for #<RSpec::ExampleGroups::User:0x007fab919f8cf
8>
     # ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'

我已经用谷歌搜索了正确的语法,但找不到解决方案。有谁知道在这里使用什么?谢谢

【问题讨论】:

标签: ruby-on-rails ruby rspec


【解决方案1】:

您必须将 gem 添加到 Gemfile:

宝石文件

group :test do
  gem 'shoulda-matchers'
end

并包含它:

spec/rails_helper.rb

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveModel, type: :model)
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end

【讨论】:

    【解决方案2】:

    我通过添加以下内容使其工作:

    宝石文件

    group :test do
      gem 'shoulda-matchers'
    end
    

    rails_helper.rb

    require 'shoulda/matchers'
    
    Shoulda::Matchers.configure do |config|
      config.integrate do |with|
        with.test_framework :rspec
        with.library :rails
      end
    end
    

    .rspec

    --color
    --require spec_helper
    --require rails_helper
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多