Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec tests failing with undefined method `validate_uniqueness_of'

With Rails 5 and RSpec 3.5, I'm getting the following error.

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

I've googled around for the right syntax but couldn't find the solution. Does anyone have an idea of what to use here? Thanks

like image 445
alpaca Avatar asked Dec 07 '16 21:12

alpaca


2 Answers

You have to add the gem to the Gemfile:

Gemfile

group :test do
  gem 'shoulda-matchers'
end

And include it:

spec/rails_helper.rb

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveModel, type: :model)
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end
like image 90
Pablo Gonzaga Avatar answered Oct 19 '22 21:10

Pablo Gonzaga


I made it work by adding this:

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
like image 33
Eduardo Lago Aguilar Avatar answered Oct 19 '22 22:10

Eduardo Lago Aguilar