ruby-on-rails – RSpec的未定义方法`validate_presence_of’:: ExampleGroups :: User :: Validations

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – RSpec的未定义方法`validate_presence_of’:: ExampleGroups :: User :: Validations前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图通过我的模型规范进行验证,但我无法让它们通过.我已将所有验证规则放在我的模型文件

应用程序/模型/ 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,uniqueness: true
  validates :token,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",$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",$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

解决方法

我认为您需要将以下内容添加到spec_helper.rb中
require 'shoulda/matchers'

如果匹配器的方法丢失了您的规格.

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

原文链接:https://www.f2er.com/ruby/267613.html

猜你在找的Ruby相关文章