如果失败,我想从测试中输出一堆信息。
当我输出信息但它出现在rspec输出的Failures:
部分之前,而不是特定规格故障信息的位置(行号等)
在rspec中是否有一种方法可以显示故障本身的信息而不是单独显示?
我以为mybe是个钩子,但......
警告:在钩子之前和之后,钩子不会与示例共享状态。这意味着你不能在周围的钩子和例子之间共享实例变量.``
您可以在测试中使用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" }