RSPEC methods Flashcards
What alias method you can use instead of ‘it’?
specify 'works' do expect(1).to eq 1 end # or example 'works' do expect(1).to eq 1 end # it's the same as 'it'
What is the base test structure?
require ‘rails_helper’
RSpec.describe Project do it 'returns true' do expect(true).to eq true end end
What is the correct order of running before
blocks?
before(:all) { p 'before all'} before(:example) { p 'before example'} it { p 'test'} after(:example) { p 'after example'} after(:all) { p 'after all'}
How to use automatically created matcher? ( 2 way )
These 2 expressions are the same
expect(project.whatever?).to be true
expect(project).to be_whatever
expect(project).to be_a_whatever
be_whatever transforms just to ask project.whatever?
How to don’t stop after the first breaking condition inside the one test?
use :aggregate_failures
it ‘does not stop after failing the first condition’, :aggregate_failures do
expect(true).to be false
expect(false).to be true
expect(1).to eq 2
# all errors will be displayed with :aggregate_failures
end
How to create and check subject? (2 way)
subject { ‘Hello’ }
it ‘checks subject’ do
expect(subject).to eq(‘Hello’)
is_expected.to eq(‘Hello’)
end
How to stub a specific date?
travel_to Date.parse(“2018-02-10”)
How to merge additional field to the hash inside context?
# use super() let(:params) { {name: 'Name'} }
context ‘with another params’ do
let(:params) { super().merge(age: 1) }
it 'returns new params' do expect(params).to have_key(:name) expect(params).to have_key(:age) end end
What are 3 ways to move common variables out of examples?
- before { @sandwich = Sandwich.new }
- let(:sandwich) { Sandwich.new }
- # ruby methoddef sandwich
@sandwich ||= Sandwich.new
end
How to create pending specs?
Just add “it” without the block
it ‘works’
How to run a hook only for the spec with metadata?
it ‘is delicious’ , sample: true
RSpec.configure do |config| config.after(:example, sample: true) do puts 'sample' end end
What hooks do you know?
before(:example)
before(:each) # the same as example - outdated
before(:context)
before(:all) # the same as context - outdated
before(:suite) # runs only once for the first group
before/after/around