使用 RSpec,有没有办法在我不知道某些值是什么的情况下比较两个哈希?我试过使用instance_of
,但它似乎不起作用。
在我的测试中,我正在创建一个带有 POST 请求的新设备对象,并希望确保我得到正确的 JSON 响应。它正在为设备创建一个 UUID(我没有使用关系数据库),但我显然不知道该值是什么,也不在乎。
post "/projects/1/devices.json", name: 'New Device'
expected = {'name' => 'New Device', 'uuid' => instance_of(String), 'type' => 'Device'}
JSON.parse(body).should == expected
我收到以下错误:
1) DevicesController API adds a new device to a project
Failure/Error: JSON.parse(body).should == expected
expected: {"name"=>"New Device", "uuid"=>#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x5a27147d @klass=String>, "type"=>"Device"}
got: {"name"=>"New Device", "uuid"=>"ef773465-7cec-48fd-b2a7-f1da10d1595a", "type"=>"Device"} (using ==)
Diff:
@@ -1,4 +1,4 @@
"name" => "New Device",
"type" => "Device",
-"uuid" => #<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x5a27147d @klass=String>
+"uuid" => "ef773465-7cec-48fd-b2a7-f1da10d1595a"
# ./spec/api/devices_spec.rb:37:in `(root)'