rspec - 如何向出现的 Failure/Error 添加更多信息
rspec - how to add more info to the Failure/Error where it occurs
如果测试失败,我想输出一堆信息。
当我发布信息时,它出现在 之前 rspec 输出的 Failures:
部分,而不是特定规范故障信息所在的位置(行号等)
规范在 rspec 中有没有办法在故障本身而不是单独显示信息?
我以为我是一个绕圈钩,但是...
WARNING: around hooks do not share state with the example the way
before and after hooks do. This means that you cannot share instance
variables between around hooks and examples.```
您可以在测试中使用 lambda:
expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: #{detailed info}." }
会给你这样的输出:
Failures:
1) Blah blah blah
Failure/Error: expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: nil." }
This failed for all sorts of reasons, let me list them.
# ./spec/features/search_results_spec.rb:19:in `block (2 levels) in <top (required)>'
如果你有像 expect(x).to eq y.count
这样的代码,这可能会有点棘手,因为只是添加 lambda 给出了 2 个给定的参数,但预期为 0..1。要解决这个问题,请使用像
这样的格式
expect(x).to (eq y.count), lambda { "message" }
如果测试失败,我想输出一堆信息。
当我发布信息时,它出现在 之前 rspec 输出的 Failures:
部分,而不是特定规范故障信息所在的位置(行号等)
规范在 rspec 中有没有办法在故障本身而不是单独显示信息?
我以为我是一个绕圈钩,但是...
WARNING: around hooks do not share state with the example the way before and after hooks do. This means that you cannot share instance variables between around hooks and examples.```
您可以在测试中使用 lambda:
expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: #{detailed info}." }
会给你这样的输出:
Failures:
1) Blah blah blah
Failure/Error: expect(page).to have_text("Doesn't exist"), lambda { "This failed for all sorts of reasons, let me list them out here: nil." }
This failed for all sorts of reasons, let me list them.
# ./spec/features/search_results_spec.rb:19:in `block (2 levels) in <top (required)>'
如果你有像 expect(x).to eq y.count
这样的代码,这可能会有点棘手,因为只是添加 lambda 给出了 2 个给定的参数,但预期为 0..1。要解决这个问题,请使用像
expect(x).to (eq y.count), lambda { "message" }