How to create and use shared_example?
shared_examples 'testable' do
it 'works' do
expect(true).to be true
end
endit_behaves_like ‘testable’
How to run shared examples? ( 2 way )
it_behaves_like ‘testable’
include_examples ‘testable’
How to rewrite let() inside shared examples?
# 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
How to define custom matcher?
RSpec::Matchers.define :be_length_equal_two do
match do |actual|
actual.size == 2
end
endit ‘checks object attributes’ do
expect(‘ss’).to be_length_equal_two
end
How to define custom matcher with an argument?
RSpec::Matchers.define :be_length_equal_to do |expected|
match do |actual|
actual.size == expected
end
endit ‘checks object attributes’ do
expect(‘ss’).to be_length_equal_to(2)
end