规范验证失败

时间:2012-08-01 04:19:58

标签: ruby-on-rails rspec

以下测试全部通过除了“描述”中的“it ​​{should be_valid}”行发送处理“do”和“describe”收到的处理“do”

require 'spec_helper'

describe Treating do

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

  before { @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") }
  before { @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") }

  describe "sent treatings" do
    subject { @sent_treating }
      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 treatings" do
    subject { @received_treating }
      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
        Treating.new(requestor_id: requestor.id)
      end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end

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

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

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

这是错误:

Failures:

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

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

最后,我的user.rb模型:

class Treating < 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

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

在这个测试中,您有两次会议,并且只在夹具中设置每个会议的两个ID中的一个:

before { @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") }
before { @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") }

假设用户与上一个问题相同,

class User < ActiveRecord::Base
  ...
  has_many :sent_meetings, :foreign_key => "requestor_id", :class_name => "Meeting"
  has_many :received_meetings, :foreign_key => "requestee_id", :class_name => "Meeting"

@received_treating应该有一个requestee_id但不是requestter_id(从未在任何地方分配过!)而且@sent_treating有一个requestor_id。

因此,出于与last question中相同的原因,验证失败,因为两者都只有验证集中请求的两个ID中的一个。

您期望的行为是什么?如果要与用户建立n:m关系,则必须在某个时刻指定第二个用户。也许你的意思是这样的夹具:

before do 
  @treating = requestor.sent_treatings.build(intro: "Lorem ipsum")
  @treating.requestee = requestee
end

也许你甚至想在Treating

中创建一个自定义setter
def send_to(user)
  requestee = user
end

然后你可以写一些类似

的东西
before { @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum").send_to(requestee) }

这为你提供了两个ID的治疗。

答案 1 :(得分:0)

使用前(:每个)块

before(:each) {
    @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") 
    @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") 
}