RSpec测试失败,未定义方法`validate_uniqueness_of'

时间:2016-12-07 21:08:16

标签: ruby-on-rails ruby rspec

使用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)>'

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

2 个答案:

答案 0 :(得分:2)

您必须将gem添加到Gemfile:

<强>的Gemfile

group :test do
  gem 'shoulda-matchers'
end

包括它:

<强>规格/ rails_helper.rb

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

答案 1 :(得分:1)

我通过添加以下内容使其成功:

<强>的Gemfile

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