0

以下测试都通过了,除了“描述“发送的处理”做和“描述“接收的处理”做'中的“it ​​{ should be_valid}”行

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

任何帮助表示赞赏!

4

2 回答 2

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 而不是 requester_id(从未在任何地方分配过!)并且@sent_treating 有一个 requestor_id。

同样,出于与上一个问题相同的原因,验证失败,因为两者都只有验证集中请求的两个 id 之一。

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

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

也许你甚至想在 Treating 中创建一个自定义设置器

def send_to(user)
  requestee = user
end

然后你可以写类似

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

这给了你一个同时设置了 id 的 Treating。

于 2012-08-01T05:23:57.817 回答
0

使用 before(:each) 块

before(:each) {
    @received_treating = requestee.received_treatings.build(intro: "Lorem ipsum") 
    @sent_treating = requestor.sent_treatings.build(intro: "Lorem ipsum") 
}
于 2012-08-02T05:28:38.910 回答