Skip to content

Instantly share code, notes, and snippets.

@gaygenius
Last active May 3, 2017 23:16
Show Gist options
  • Save gaygenius/2c8b42cfe1a50b2259eb45db2d929aa3 to your computer and use it in GitHub Desktop.
Save gaygenius/2c8b42cfe1a50b2259eb45db2d929aa3 to your computer and use it in GitHub Desktop.
Correct use of rspec matching
describe 'Expectations' do
it 'fails with the wrong expectations' do
my_hash = { 'created_at' => Time.now }
expect(my_hash['created_at']).to be_a_kind_of(Time)
# expect(my_hash)
# .to eql({ 'created_at' => a_kind_of(Time) })
# expect(my_hash)
# .to eq({ 'created_at' => a_kind_of(Time) })
array_of_hashes = [ my_hash ]
# expect(array_of_hashes)
# .to eql([ { 'created_at' => a_kind_of(Time) } ])
# expect(array_of_hashes)
# .to eq([ { 'created_at' => a_kind_of(Time) } ])
end
it 'works with the right expectations' do
my_hash = { 'created_at' => Time.now }
expect(my_hash['created_at']).to be_a_kind_of(Time)
expect(my_hash)
.to match({ 'created_at' => a_kind_of(Time) })
array_of_hashes = [ my_hash ]
expect(array_of_hashes)
.to contain_exactly({ 'created_at' => a_kind_of(Time) })
expect(array_of_hashes)
.to match_array([ { 'created_at' => a_kind_of(Time) } ])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment