rspec测试失败:两个“主题”之间的冲突

时间:2012-07-31 16:05:27

标签: ruby-on-rails rspec

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

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

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"
end

我的meeting.rb模型:

meeting.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

我的测试:

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") }

  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)会议请求者      失败/错误:它(:请求者){should == requestor}        预期:#             得到:零(使用==)      #./spec/models/meeting_spec.rb:15:in'块(2级)in'

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

失败的例子:

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

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

如何让测试通过?谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

会更快。