RSpec :: ExampleGroups :: User :: Validations的未定义方法`validate_presence_of'

时间:2015-07-07 18:33:16

标签: ruby-on-rails ruby-on-rails-4 rspec capybara

我正在尝试通过我的模型规范进行验证,但我无法让它们通过。我已将所有验证规则放在我的模型文件中

应用/模型/ 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

规格/模型/ 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

规格/工厂/ 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 "ralph@thoughtbot.com"
    facebook_user_id { generate(:facebook_id) }
    facebook_user_token

    birthday { Date.current }
    gender "Male"

    token { Token.new.generate }
  end

end

测试输出

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)=(ralph@thoughtbot.com) 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)=(ralph@thoughtbot.com) 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)=(ralph@thoughtbot.com) 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

3 个答案:

答案 0 :(得分:9)

$m[2]

中添加rspec-railsshoulda-matchers
Gemfile

group :development, :test do gem 'rspec-rails', '~> 3.4', '>= 3.4.2' gem 'shoulda-matchers', '~> 3.1', '>= 3.1.1' end 设置为使用spec/rails_helper.rbrspec-rails

shoulda-matchers

并添加require 'rspec/rails' require 'shoulda/matchers'

spec/spec_helper.rb

答案 1 :(得分:4)

我认为您需要将以下内容添加到spec_helper.rb

require 'shoulda/matchers'

如果匹配器的方法不符合您的规格。

这是来源。你可以检查一下。人们抱怨类似的事情。 https://github.com/thoughtbot/shoulda-matchers/issues/384

答案 2 :(得分:0)

在您的gem文件中添加gem:

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

/spec/spec_helper.rb

RSpec.configure do |config|
  require 'shoulda/matchers' #add this line in it. 
  .....
  ...
end

/spec/rails_helper.rb

#add this code
    Shoulda::Matchers.configure do |config|
      config.integrate do |with|
        with.test_framework :rspec
        with.library :rails
      end
    end