rspec failing validations

可紊 提交于 2019-12-25 05:25:18

问题


i have a meeting.rb and user.rb model

meeting.rb

class Meeting < ActiveRecord::Base
  attr_accessible :intro, :proposed_date, :proposed_location

  validates :requestor_id, presence: true
  validates :requestee_id, presence: true

  belongs_to :requestor, class_name: "User"
  belongs_to :requestee, class_name: "User"

end

user.rb

class User < ActiveRecord::Base
    attr_accessible :name, :email, :password, :password_confirmation
    has_secure_password

    has_many :sent_meetings, :foreign_key => "requestor_id", :class_name => "Meeting"
    has_many :received_meetings, :foreign_key => "requestee_id", :class_name => "Meeting"

I cannot figure out how to prevent my meeting_spec.rb validations from failing. For some reason, my sent_meetings or requested_meetings (class Meeting) are not linking to their respective requestor or requestee (class User) (last two blocks of code, "when requestor_id is not present", "when requestee_id is not present" do).

meeting_spec.rb

require 'spec_helper'

describe Meeting do

  let(:requestee) { FactoryGirl.create(:user) }
  let(:requestor) { FactoryGirl.create(:user) }

  before { @received_meeting = requestee.received_meetings.build(intro: "Lorem ipsum") }
  before { @sent_meeting = requestor.sent_meetings.build(intro: "Lorem ipsum") }

  describe "sent meetings" do
    subject { @sent_meeting }
      it { should respond_to(:intro) }
      it { should respond_to(:requestor_id) }
      it { should respond_to(:requestor) }
      its(:requestor) { should == requestor }
      it { should be_valid }
  end

  describe "received meetings" do
    subject { @received_meeting }
      it { should respond_to(:intro) }
      it { should respond_to(:requestee_id) }
      it { should respond_to(:requestee) }
      its(:requestee) { should == requestee }
      it { should be_valid }
  end

  describe "accessible attributes" do
    it "should not allow access to requestor_id" do
      expect do
        Meeting.new(requestor_id: requestor.id)
      end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end

    it "should not allow access to requestee_id" do
      expect do
        Meeting.new(requestee_id: requestee.id)
      end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end
  end

  describe "when requestor_id is not present" do
    before { @sent_meeting.requestor_id = nil }
    it { should be_valid }
  end

  describe "when requestee_id is not present" do
    before { @received_meeting.requestee_id = nil }
    it { should be_valid }
  end
end

Here is the rspec error I get:

Failures:

  1) Meeting sent meetings 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/meeting_spec.rb:17:in `block (3 levels) in <top (required)>'

  2) Meeting received meetings
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/meeting_spec.rb:26:in `block (3 levels) in <top (required)>'

Finished in 0.77435 seconds
42 examples, 2 failures

Failed examples:

rspec ./spec/models/meeting_spec.rb:17 # Meeting sent meetings 
rspec ./spec/models/meeting_spec.rb:26 # Meeting sent meetings 

Would really appreciate any input on how to make this association between User (requestor, requestee) and Meeting (sent_meeting, requested_meeting).

Thank you!


回答1:


in your model you're requesting the presence of both ids with

  validates :requestor_id, presence: true
  validates :requestee_id, presence: true

so if you nil one of them as in your spec

 describe "when requestor_id is not present" do
    before { @sent_meeting.requestor_id = nil }
    it { should be_valid }
  end

they are invalid, not valid, as you declared them not to be valid in this case.



来源:https://stackoverflow.com/questions/11750715/rspec-failing-validations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!