在 RSpec 和 Cucumber 中轻松处理 JSON
在 RSpec 和 Cucumber 中轻松处理 JSON
json_spec 定义了五个新的 RSpec 匹配器:
be_json_eqlinclude_jsonhave_json_pathhave_json_typehave_json_size
新的匹配器可以在 RSpec 中如下使用:…json_spec 还为 RSpec 测试提供了一些有用的辅助函数:
parse_jsonnormalize_jsongenerate_normalized_jsonload_json
要开始使用它们,请在 RSpec 配置中添加它们:RSpec.configure do |config|
config.include JsonSpec::Helpers
end您可以在 spec/json_spec/helpers_spec.rb 中找到这些辅助函数的使用示例
在比较 JSON 时,json_spec 默认会忽略某些哈希键:
idcreated_atupdated_at在评估新创建的 ActiveRecord 记录的 JSON 表示时,有时会很有用,这样就不必知道新的 ID 和时间戳。这些排除可以全局自定义:
{ "first_name": "Steve", "last_name": "Richert", "friends": [ { "first_name": "Catie", "last_name": "Richert" } ] }
我们可以通过路径 "friends/0/first_name" 访问第一个朋友的名字。
暂无开放 Issues,或尚未同步最近议题。