RSPEC expectation Flashcards
1
Q
How to check values on true/false? ( 2 way )
A
# passes if actual is truthy (not nil or false) expect(actual).to be_truthy # passes if actual == true expect(actual).to be true
# passes if actual is falsy (nil or false) expect(actual).to be_falsey # passes if actual == false expect(actual).to be false
2
Q
How to check the attributes of the object?
A
expect(actual_object).to have_attributes(name: 'Nic', age:12) # or the alias expect(actual_object).to has_attributes(name: 'Nic', age:12)
3
Q
How to check object attributes value of another object?
A
use an_object_having_attributes
let(:task) { Task.new(size: 2) } let(:project) { Project.new(task: task) } it 'checks object attributes' do expect(project).to have_attributes(task: an_object_having_attributes(size: 2)) end
4
Q
How to check array values?
A
expect([1,2,3]).to match([1,2,3]) # exactly the same with correct order
expect([1,2,3]).to contain_exactly(3, 2, 1) # exactly the same without order
5
Q
How to check error raising?
A
expect {
subject # or any other action
}.to raise_error(NotFoundError)
6
Q
How to check hash values?
A
expect(hash).to a_hash_including(‘employee_minimum’ => 1)
7
Q
How to check that hash include a key?
A
expect(hash).to have_key(:key)