ruby - How to define a negated description for a custom matcher
问题描述
When i test stuff with rspec/ruby i use a lot of custom matchers. One of the big advandages for me it that i can create custom failure messages and descriptions. I use the descriptions to print out the expected behaviour at the bgeinning of the test. But i have a problem when a negate matchers.
I would like to define a description_when_negated. Is their a way to know if a matcher is negated in the description part? Or can i know it in the match part and print out somthing different.
Simple code example:
Spec::Matchers.define :have_value do |expected_value|
match do |actual|
puts "Expect: #{description}"
actual.value == expected_value
end
description do
"Object'#{actual.id}' should have value '#{expected_value}'"
end
failure_message do |actual|
"Object'#{actual.id}' has value '#{actual.value}' instead of '#{expected_value}'"
end
failure_message_when_negated do |actual|
"Object'#{actual.id}' has value '#{actual.value}' and that was not expected"
end
end
Test:
expect(object1).to have_value(1235)
expect(object1).to have_name('test')
expect(object1).to_not have_description('test')
Output:
Expect Object 1 to have value '1235'
Expect Object 1 to have name 'test'
Expect Object 1 to NOT have description 'test'
解决方案
推荐阅读
- python - '在
' 需要字符串作为左操作数,而不是列表 - selenium - 输入时如何从 Selenium 的下拉菜单中选择值
- java - 为什么代码在 LIBGDX 中的 setScreen() 之后运行?
- angularjs - 注入组件导致 Unknown provider $scopeProvider
- java - 如何在 ARCore 中获取最新的 Detected Plane?
- flutter - 打开键盘时如何显示完整的showModalBottomSheet
- python - for循环如何处理python中的字符串值?
- mysql - 使用 Reg ex 的 SQL GROUP BY 名称
- ios - Unix dateTime 格式未在 swift 5 中正确转换
- php - 检查参数是否等于模型中定义的常量