rspec验证失败

时间:2012-07-31 23:54:06

标签: ruby-on-rails rspec

我有一个meeting.rb和user.rb模型

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"

我无法弄清楚如何防止我的meeting_spec.rb验证失败。出于某种原因,我的sent_meetings或requested_meetings(类会议)没有链接到他们各自的请求者或被请求者(类用户)(最后两个代码块,&#34;当requestor_id不存在时#34;,&#34;当requestedtee_id不存在&#34; 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

这是我得到的rspec错误:

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 

非常感谢有关如何在用户(请求者,被请求者)和会议(sent_meeting,requested_meeting)之间建立此关联的任何输入。

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的模型中,您要求使用

存在两个ID
  validates :requestor_id, presence: true
  validates :requestee_id, presence: true

所以,如果你在规范中没有其中一个

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

它们无效,无效,因为您声明它们在这种情况下无效。