为匹配中的多个expect语句定制断言消息

时间:2014-07-09 21:24:01

标签: ruby rspec

我在Rspec中编写了一个自定义匹配方法,用于将对象与哈希匹配。我要做的是为expect的每一行设置自定义失败消息。

describe "/cars" do
  car = FactoryGirl.create(:car, name: 'Alpha')
  describe car do
    it "displays single items" do
      get cars_path
      parsed_response = JSON.parse(response.body)
      record_hash = parsed_response['cars'][0]
      is_expected.to be_a_car_match_of(record_hash)
    end
  end
end

RSpec::Matchers.define :be_a_car_match_of do |hash|
  match do |car|
    expect(car.id).to    eq(hash['id'])
    expect(car.name).to  eq(hash['name'])
  end
  failure_message do |car|
    "expected that #{car} would be a match of #{hash}"
  end
end

所以我想要的是拥有以下内容:

RSpec::Matchers.define :be_a_car_match_of do |hash|
  match do |car|
    expect(car.id).to    eq(hash['id'])    'ids did not match'
    expect(car.name).to  eq(hash['name'])  'names did not match'
  end
end

这将打印出更清晰的错误消息。

我最初是在迷你测试中做这个,但由于各种原因(我无法控制)需要将其更改为rspec。我在mini-test中的代码是:

def assert_car(car, hash)
    assert_equal car.id,    hash['id'],   "ids did not match"
    assert_equal car.name,  hash['name'], "names did not match"
end

这就是我想要复制的内容。

这是另一个需要较少设置的示例:

require 'rspec/expectations'

RSpec::Matchers.define :be_testing do |expected|
  match do |actual|
    expect(5).to eq(5)
    expect(4).to eq(5)
  end
  failure_message do
    "FAIL"
  end
end

describe 'something' do
  it 'something else' do
    expect("expected").to be_testing('actual')
  end
end

运行此示例时," FAIL"打印出来。另一方面,如果我有:

describe 'something' do
  it 'something else' do
    expect(4).to eq(5)
  end
end

我会收到以下错误消息:

expected: 5
     got: 4

这就是我想要的。我想知道自定义匹配器的哪个部分失败了。

2 个答案:

答案 0 :(得分:2)

您可以调用低级matches?方法而不是expect。像这样:

require 'rspec/expectations'

RSpec::Matchers.define :be_a_positive_integer do

  m1, m2 = nil, nil           # matchers
  r1, r2 = false, false       # results

  match do |actual|
    m1 = be_a Integer         # this returns matcher instances
    m2 = be > 0

    r1 = m1.matches?(actual)  # evaluate matchers
    r2 = m2.matches?(actual)

    r1 && r2                  # true if both are true
  end

  failure_message do |actual| # collect error messages from matchers
    messages = []
    messages << m1.failure_message unless r1
    messages << m2.failure_message unless r2
    messages.join("\n")
  end

end

describe -1 do
  it { is_expected.to be_a_positive_integer }
end

describe 1.0 do
  it { is_expected.to be_a_positive_integer }
end

describe -1.0 do
  it { is_expected.to be_a_positive_integer }
end

输出:

Failures:

  1) -1 should be a positive integer
     Failure/Error: it { is_expected.to be_a_positive_integer }
       expected: > 0
            got:   -1
     # ./ruby_spec.rb:24:in `block (2 levels) in <top (required)>'

  2) 1.0 should be a positive integer
     Failure/Error: it { is_expected.to be_a_positive_integer }
       expected 1.0 to be a kind of Integer
     # ./ruby_spec.rb:28:in `block (2 levels) in <top (required)>'

  3) -1.0 should be a positive integer
     Failure/Error: it { is_expected.to be_a_positive_integer }
       expected -1.0 to be a kind of Integer
       expected: > 0
            got:   -1.0
     # ./ruby_spec.rb:32:in `block (2 levels) in <top (required)

答案 1 :(得分:0)

我认为aggregate_failures正是您所寻找的:

  

它用一个块来包含一系列期望。在块内,期望失败不会像正常一样立即中止;相反,失败将聚合成一个在块结束时引发的异常,允许您查看失败的所有期望。

请参阅:https://relishapp.com/rspec/rspec-expectations/docs/aggregating-failures