【问题标题】:Is it possible to change Rspec output on objects?是否可以更改对象的 Rspec 输出?
【发布时间】:2013-02-07 19:53:05
【问题描述】:

鉴于以下规格:

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

我可以将#&lt;Person person_id: ....&gt; 更改为其他内容吗?

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

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

【问题讨论】:

    标签: unit-testing rspec rspec-rails


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-05-30
        • 2018-05-15
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        • 1970-01-01
        • 2015-08-18
        • 2014-05-31
        • 2016-06-24
        相关资源
        最近更新 更多