RSpec partial match against a nested hash

前端 未结 2 994
梦如初夏
梦如初夏 2021-02-05 05:02

I\'ve got a JSON structure that I\'d like to match a single nested element in, while ignoring other data. The JSON looks like this (minimally):

{
  \"employee\":         


        
2条回答
  •  孤独总比滥情好
    2021-02-05 05:28

    You are able to use and nest the hash_including method for these matchers.

    Using your example, you can rewrite your test code to look like:

    expect(response_json).to include(hash_including({
      employee: hash_including(jobs_count: 0)
    }))
    

    (or if response_json is a single object, replace include with match)

    This will also work when dealing with .with constraints, for example:

    expect(object).to receive(:method).with(hash_including(some: 'value'))
    

提交回复
热议问题