Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `validate_presence_of' for RSpec::ExampleGroups::User::Validations

I am trying to pass my Model Specs for validation but i am not able to get them to pass. I have placed all the validation rules in my Model file as well

app/model/user.rb

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships
  has_many :plans
  has_many :memberships, class_name: "PlanMemebership"
  has_many :notes
  has_many :replies, class_name: "NoteReply"
  has_many :upvotes, class_name: "NoteUpvote"

  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :email, presence: true
  validates :facebook_user_id, presence: true, uniqueness: true
  validates :facebook_user_token, presence: true, uniqueness: true
  validates :token, presence: true, uniqueness: true
  validates :birthday, presence: true
  validates :gender, presence: true

  def name
    first_name + " " + last_name
  end

end

specs/models/user_spec.rb

require 'rails_helper'

describe User, :type => :model do

  context "valid Factory" do
    it "has a valid factory" do
      expect(FactoryGirl.build(:user)).to be_valid
    end
  end

  context "validations" do
    it { should validate_presence_of :first_name }
    it { should validate_presence_of :last_name }
    it { should validate_presence_of :email }
    it { should validate_presence_of :birthday }
    it { should validate_presence_of :gender }
    it { should validate_presence_of :facebook_user_id }
    it { should validate_presence_of :facebook_user_token }

    context "uniqueness" do
      before { create(:user) }
      it { should validate_uniqueness_of :facebook_user_id }
      it { should validate_uniqueness_of :facebook_user_token }
      it { should validate_uniqueness_of :token }
    end
  end
end

spec/factories/users.rb

FactoryGirl.define do
  sequence(:facebook_id) { |n| "#{n}1004453333331" }
  sequence(:facebook_user_token) { |n| "#{n}CAA" + ("X" * 216) }

  factory :user do
    first_name "Ralph"
    last_name "Robot"
    email "[email protected]"
    facebook_user_id { generate(:facebook_id) }
    facebook_user_token

    birthday { Date.current }
    gender "Male"

    token { Token.new.generate }
  end

end

Test Output

Token
  #generate
    returns a 20-byte token

User
  valid Factory
    has a valid factory
  validations
    example at ./spec/models/user_spec.rb:13 (FAILED - 1)
    example at ./spec/models/user_spec.rb:14 (FAILED - 2)
    example at ./spec/models/user_spec.rb:15 (FAILED - 3)
    example at ./spec/models/user_spec.rb:16 (FAILED - 4)
    example at ./spec/models/user_spec.rb:17 (FAILED - 5)
    example at ./spec/models/user_spec.rb:18 (FAILED - 6)
    example at ./spec/models/user_spec.rb:19 (FAILED - 7)
    uniqueness
      example at ./spec/models/user_spec.rb:23 (FAILED - 8)
      example at ./spec/models/user_spec.rb:24 (FAILED - 9)
      example at ./spec/models/user_spec.rb:25 (FAILED - 10)

Failures:

  1) User validations
     Failure/Error: it { should validate_presence_of :first_name }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255f7fb88>
     # ./spec/models/user_spec.rb:13:in `block (3 levels) in <top (required)>'

  2) User validations
     Failure/Error: it { should validate_presence_of :last_name }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255e9fad8>
     # ./spec/models/user_spec.rb:14:in `block (3 levels) in <top (required)>'

  3) User validations
     Failure/Error: it { should validate_presence_of :email }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255ded568>
     # ./spec/models/user_spec.rb:15:in `block (3 levels) in <top (required)>'

  4) User validations
     Failure/Error: it { should validate_presence_of :birthday }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255d65398>
     # ./spec/models/user_spec.rb:16:in `block (3 levels) in <top (required)>'

  5) User validations
     Failure/Error: it { should validate_presence_of :gender }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255cb7888>
     # ./spec/models/user_spec.rb:17:in `block (3 levels) in <top (required)>'

  6) User validations
     Failure/Error: it { should validate_presence_of :facebook_user_id }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255c356f8>
     # ./spec/models/user_spec.rb:18:in `block (3 levels) in <top (required)>'

  7) User validations
     Failure/Error: it { should validate_presence_of :facebook_user_token }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User::Validations:0x007fc255b86a40>
     # ./spec/models/user_spec.rb:19:in `block (3 levels) in <top (required)>'

  8) User validations uniqueness
     Failure/Error: before { create(:user) }
     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
       DETAIL:  Key (email)=([email protected]) already exists.
       : INSERT INTO "users" ("first_name", "last_name", "email", "facebook_user_id", "facebook_user_token", "birthday", "gender", "token", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
     # ./spec/models/user_spec.rb:22:in `block (4 levels) in <top (required)>'

  9) User validations uniqueness
     Failure/Error: before { create(:user) }
     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
       DETAIL:  Key (email)=([email protected]) already exists.
       : INSERT INTO "users" ("first_name", "last_name", "email", "facebook_user_id", "facebook_user_token", "birthday", "gender", "token", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
     # ./spec/models/user_spec.rb:22:in `block (4 levels) in <top (required)>'

  10) User validations uniqueness
     Failure/Error: before { create(:user) }
     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
       DETAIL:  Key (email)=([email protected]) already exists.
       : INSERT INTO "users" ("first_name", "last_name", "email", "facebook_user_id", "facebook_user_token", "birthday", "gender", "token", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
     # ./spec/models/user_spec.rb:22:in `block (4 levels) in <top (required)>'

Finished in 0.13486 seconds (files took 3.99 seconds to load)
12 examples, 10 failures

Failed examples:

rspec ./spec/models/user_spec.rb:13 # User validations
rspec ./spec/models/user_spec.rb:14 # User validations
rspec ./spec/models/user_spec.rb:15 # User validations
rspec ./spec/models/user_spec.rb:16 # User validations
rspec ./spec/models/user_spec.rb:17 # User validations
rspec ./spec/models/user_spec.rb:18 # User validations
rspec ./spec/models/user_spec.rb:19 # User validations
rspec ./spec/models/user_spec.rb:23 # User validations uniqueness
rspec ./spec/models/user_spec.rb:24 # User validations uniqueness
rspec ./spec/models/user_spec.rb:25 # User validations uniqueness
like image 488
Harsha M V Avatar asked Jul 07 '15 18:07

Harsha M V


2 Answers

Add rspec-rails and shoulda-matchers in your Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> 3.4', '>= 3.4.2'
  gem 'shoulda-matchers', '~> 3.1', '>= 3.1.1'
end

Set your spec/rails_helper.rb to use rspec-rails and shoulda-matchers:

require 'rspec/rails'
require 'shoulda/matchers'

And add in your spec/spec_helper.rb:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
like image 137
monteirobrena Avatar answered Sep 28 '22 01:09

monteirobrena


I think you need to add the following to your spec_helper.rb

require 'shoulda/matchers'

Your specs are missing shoulda matcher's methods.

Here is the source. You can check. People complains similar things. https://github.com/thoughtbot/shoulda-matchers/issues/384

like image 21
Rubyrider Avatar answered Sep 28 '22 02:09

Rubyrider