是否可以更改对象的Rspec输出?

时间:2013-02-07 19:53:05

标签: unit-testing rspec rspec-rails

鉴于以下规范:

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

使用格式--documentation:

在CLI中输出
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?

此外,如果可以在每个测试的基础上进行自定义,而不是在类级别更改。

2 个答案:

答案 0 :(得分:1)

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

describe ".senior" do
  subject { Person.senior }
  it { should include(sixty_older), "should include #{person.person_id}:#{person.uuid}" }
end

答案 1 :(得分:0)

在每个示例或上下文或您喜欢的任何内容之前,尝试覆盖模型类的 inspect 实例方法。我有同样的需求测试ActiveRecord模型与Rspec,它的工作原理。