1

鉴于以下规格:

describe Person
  describe "scopes and class methods" do
    let!(:sixty_older)     { FactoryGirl.create :person, birthdate: 60.years.ago }

    describe ".senior" do
      subject { Person.senior }
      it { should include(sixty_older) }
    end
  end
end

它在 CLI 中使用格式 --documentation 的输出:

Person
  scopes and class methods
    .senior
      should include #<Person person_id: 466, gender: "M", birthdate: "1953-02-07 19:44:22", birthdate_estimated: 0, dead: 0, death_date: nil, cause_of_death: nil, creator: 0, date_created: "0002-11-30 00:00:00", changed_by: nil, date_changed: nil, voided: 0, voided_by: nil, date_voided: nil, void_reason: nil, uuid: "key_4">

我可以将其更改#<Person person_id: ....>为其他内容吗?

比如说,只显示 person_id、birthdate 和 uuid?

此外,如果它可以在每个测试的基础上进行定制,而不是在类级别的变化上。

4

2 回答 2

1

您可以向大多数 rspec 匹配器传递自定义失败消息,如下所示:

describe ".senior" do
  subject { Person.senior }
  it { should include(sixty_older), "should include #{person.person_id}:#{person.uuid}" }
end
于 2013-02-07T20:47:36.683 回答
0

尝试在每个示例或上下文或您喜欢的任何内容之前覆盖模型类的检查实例方法。我同样需要使用 Rspec 测试 ActiveRecord 模型并且它有效。

于 2013-05-16T09:19:06.860 回答