RSpec 匹配响应

RSpec matching response

我在 Rails 5 和 Rspec 上使用 Ruby。

我的测试是这样的

expect(json_response['data']['body']).to match(/'["can't be blank"]'/)

我遇到错误

expected ["can't be blank"] to match /'["can't be blank"]'/

我想知道,如何解决?希望清楚。

尝试 match_array 辅助方法。

expect(json_response['data']['body']).to match_array(["can't be blank"])

读取测试失败,JSON 响应 returns 一个包含字符串的数组:["can't be blank"]。似乎是直接测试相等性的一个很好的用例:

expect(json_response['data']['body']).to eq(["can't be blank"])

match_array 可以,但它 "disregards differences in the ordering between the actual and expected array." 作为一个只有一项的数组,这里不需要该功能。

contains_exactly/match_array docs