0

我无法通过考试。在我的应用程序中,用户可以拥有许多他们请求的会议 (sent_meetings) 和请求的会议 (received_meetings)。

首先,我的(简化的)user.rb 模型:

用户.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"
end

还有我的 meeting.rb 模型:

会议.rb

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

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

我的测试:

会议规范.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") }

  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 }

  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

我的规范文件(@sent_meeting 和@received_meeting)中的两个“主题”行之间似乎存在冲突,并且其中一个覆盖了另一个。这是我失败的测试消息:

失败:

1) 会议请求者失败/错误: its(:requestor) { should == requestor } expected: # got: nil (using ==) # ./spec/models/meeting_spec.rb:15:in `block (2 levels)在 '

在 0.67458 秒内完成 36 个示例,1 个失败

失败的例子:

rspec ./spec/models/meeting_spec.rb:15 # 会议请求者

我发现有趣的是,我只为请求者或被请求者收到此错误显然存在冲突)。两个主题代码块的顺序很重要,当我切换它们时,我得到与上面相同的错误,但关于“请求者”而不是“请求者”。

我怎样才能让测试通过?谢谢!

4

1 回答 1

2

你不能这样做。你必须这样做:

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

end

您也可以像这样使用 let 代替:

require 'spec_helper'

describe Meeting do

  let(:requestee) { FactoryGirl.create(:user) }
  let(:requestor) { FactoryGirl.create(:user) }
  let(:received_meeting) { requestee.received_meetings.build(intro: "Lorem ipsum") }
  let(: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

end

它会更快。

于 2012-07-31T16:09:01.063 回答