我正在写类似这样的Rspec期望值:
expect(result['data']['register']['registered']).to be(false), result.inspect
问题是,大多数情况下,出现问题时出现的错误是:
undefined method `[]' for nil:NilClass
我发现了这种更好的书写方式:
expect(result.dig('data', 'register', 'registered')).to be(false), result.inspect
至少显示result.inspect
的结果,但我想会有更好的结果,例如:
expect(result).to dig('data', 'register', 'registered').and be(false), result.inspect
我曾考虑过使用hash_including
,但无法使其正常工作。我尝试过:
expect(result).to eq(hash_including("data" => {"register" => {'registered' => be(false)}}))
expect(result).to eq(hash_including("data" => {"register" => {'registered' => false}}))
expect(result).to eq(hash_including("data" => hash_including("register" => hash_including('registered' => be(false)))))
expect(result).to eq(hash_including("data" => hash_including("register" => hash_including('registered' => false))))
[第二个工作都不是很冗长...我认为即使第一个也不如dig
好,但我离题了。
你看过hash_including吗?
a = {a: 2}
c = {b: 4}
expect(a).to eq(hash_including(c))
这是输出:
expected: hash_including(:b=>4)
got: {:a=>2}