RSpec shared_example/matcher Flashcards
1
Q
How to create and use shared_example?
A
shared_examples 'testable' do it 'works' do expect(true).to be true end end
it_behaves_like ‘testable’
2
Q
How to run shared examples? ( 2 way )
A
it_behaves_like ‘testable’
include_examples ‘testable’
3
Q
How to rewrite let() inside shared examples?
A
# send new let inside the block shared_examples 'testable' do let(:sample) { 'Sample' }
it 'works' do expect(sample).to eq 'Sample From Shared Example Block' end end
it_behaves_like ‘testable’ do
let(:sample) { ‘Sample From Shared Example Block’ }
end
4
Q
How to define custom matcher?
A
RSpec::Matchers.define :be_length_equal_two do match do |actual| actual.size == 2 end end
it ‘checks object attributes’ do
expect(‘ss’).to be_length_equal_two
end
5
Q
How to define custom matcher with an argument?
A
RSpec::Matchers.define :be_length_equal_to do |expected| match do |actual| actual.size == expected end end
it ‘checks object attributes’ do
expect(‘ss’).to be_length_equal_to(2)
end