I am testing a JSON API, and right now I do this:
expect(json).to have_key('name')
expect(json).to have_key('age')
How can I make sure that the JSON has just the keys name
and age
, and no other keys?
Use the #contain_exactly
matcher:
expect(json.keys).to contain_exactly('name', 'age')
Spec:
describe "Hash" do
subject { {a: 2, b: 3} }
it "passes" do
expect(subject.keys).to contain_exactly(:a, :b)
end
end
Let's run it :
arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
.
Finished in 0.00227 seconds (files took 0.13131 seconds to load)
1 example, 0 failures
Spec:
describe "Hash" do
subject { {a: 2, b: 3} }
it "fails" do
expect(subject.keys).to contain_exactly(:a)
end
end
Let's run it:
arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
F
Failures:
1) Hash fails
Failure/Error: expect(subject.keys).to contain_exactly(:a)
expected collection contained: [:a]
actual collection contained: [:a, :b]
the extra elements were: [:b]
# ./spec/test_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.00243 seconds (files took 0.13206 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/test_spec.rb:6 # Hash fails
Spec:
describe "Hash" do
subject { {a: 2, b: 3, c: 4} }
it "fails" do
expect(subject.keys).to contain_exactly(:a, :b)
end
end
Let's run it:
arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
F
Failures:
1) Hash fails
Failure/Error: expect(subject.keys).to contain_exactly(:a, :b)
expected collection contained: [:a, :b]
actual collection contained: [:a, :b, :c]
the extra elements were: [:c]
# ./spec/test_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.00243 seconds (files took 0.13301 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/test_spec.rb:6 # Hash fails
arup@linux-wzza:~/Ruby>
When dealing with JSON keys, the splat operator (*
) comes in handy to manage a list of arguments.
Spec:
describe "Hash" do
subject { {a: 2, b: 3} }
let(:json_keys) { %w{a b} }
it "passes" do
expect(subject.keys).to contain_exactly(*json_keys)
end
end
Let's run it :
arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
.
Finished in 0.00227 seconds (files took 0.13131 seconds to load)
1 example, 0 failures
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With