0

我正在测试一个基于 json 的方法。该方法采用 json 数组列表。当 json 数组按顺序出现时,该方法工作正常,而当数组随机化时,该方法会中断。它不会对所有随机案例都失败。所以,失败时我想保存 json 数组的值。有没有办法做到这一点?

describe 'when the flat hash comes in random order' do
  it 'knows how to create the parent nodes first' do
    do_stuff_and_validate(@flat_hash[:nodeList].shuffle!)
  end
end
4

3 回答 3

1

您可以定义一个自定义匹配器并覆盖失败消息以显示您想要的内容。

以下示例从RSpec 文档复制:

require 'rspec/expectations'

RSpec::Matchers.define :be_a_multiple_of do |expected|
  match do |actual|
    actual % expected == 0
  end
  failure_message_for_should do |actual|
    "expected that #{actual} would be a multiple of #{expected}"
  end
end

# fail intentionally to generate expected output
describe 9 do
  it {should be_a_multiple_of(4)}
end
于 2012-05-02T20:02:21.493 回答
0

您可以将此数组写入文件以便稍后检查。

File.open("hash.txt", 'w') do |f|
  f.write(your_json_array)
end
于 2012-05-02T19:19:37.800 回答
0

我使用了 begin-rescue-reraise 技巧。

begin
  # The assertions
rescue Exception => e
  pp @flat_hash
  raise e
end
于 2012-05-03T00:20:43.107 回答