5

When I run rspec I get this error:

Failures:

  1) User
    Failure/Error: it { should validate_presence_of :email  }

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

Finished in 0.00168 seconds (files took 2.72 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:5 # User

But how can fix it?

This is my Gemfile:

group :development, :test do
 gem 'byebug'
 gem 'rspec-rails', '~> 3.4', '>= 3.4.1'
 gem "factory_girl_rails", "~> 4.0"
 gem 'capybara', '~> 2.6', '>= 2.6.2'
 # Call 'byebug' anywhere in the code to stop execution and get a       debugger console
gem 'byebug'
end

group :test do
  gem 'shoulda-matchers', require: false
end

This is my model:

class User < ActiveRecord::Base
  validates :email, presence: true
end

And my user_spec:

require 'rails_helper'

 RSpec.describe User, type: :model do
   #pending "add some examples to (or delete) #{__FILE__}"
   it { should validate_presence_of :email  }   

 end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rul
  • 157
  • 1
  • 2
  • 10
  • [May be of help](https://github.com/thoughtbot/shoulda/issues/203) – potashin Feb 01 '16 at 23:58
  • Possible duplicate of [undefined method \`validate\_presence\_of' for RSpec::ExampleGroups::User::Validations](http://stackoverflow.com/questions/31276625/undefined-method-validate-presence-of-for-rspecexamplegroupsuservalidati) – Phiter Feb 01 '16 at 23:58
  • 2
    Welcome to Stack Overflow. I'd recommend reading http://catb.org/esr/faqs/smart-questions.html. It's chock-full of great tips for you. It's not necessary to say "Help me please" since that's what this site is for. We expect well written questions, not begging. Please take the time to format your question and use appropriate capitalization; Those pay off. – the Tin Man Feb 01 '16 at 23:59
  • Did my answer work for you? please let us know. and consider mark as best answer. Thanks – svelandiag Feb 02 '16 at 01:49
  • Thanks for your answers and recommendations :) – Rul Feb 13 '16 at 17:51

3 Answers3

11

Check if you have added configuration in rails_helper.

if require 'shoulda/matchers' doesn't work, add following config in spec/rails_helper.rb

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

For more information, refer shoulda-matchers.

Datt
  • 851
  • 9
  • 21
5

OUTDATED:

Please consider using this

Add this to your rails_helper.rb

require 'shoulda/matchers'

Your specs are missing shoulda matcher's methods.

Please take a look to this thread. Hope this helps

svelandiag
  • 4,231
  • 1
  • 36
  • 72
  • 1
    Thank you single add in shoulda_matchers_helper.rb this: Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec with.library :rails end end – Rul Feb 13 '16 at 17:50
  • 1
    It doesn't work anymore, please consider using https://stackoverflow.com/a/61292450/1240330 – DjimOnDev Aug 16 '21 at 14:17
2

If you're testing a model or a form (meaning that your form has model-like properties by including include ActiveModel::Model) - specifying the type of spec file will help remove this issue, i.e. type: :model

RSpec.describe TestForm, type: :model do
  subject { described_class.new(user_id) }
  let(:user_id) { 1 }

  it { is_expected.to validate_presence_of(:whatever) }
end

anna
  • 65
  • 6