RSPEC methods Flashcards

1
Q

What alias method you can use instead of ‘it’?

A
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'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the base test structure?

A

require ‘rails_helper’

RSpec.describe Project do
  it 'returns true' do
    expect(true).to eq true
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the correct order of running before blocks?

A
before(:all) { p 'before all'}
  before(:example) { p 'before example'}
  it { p 'test'}
  after(:example) { p 'after example'}
  after(:all) { p 'after all'}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to use automatically created matcher? ( 2 way )

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to don’t stop after the first breaking condition inside the one test?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create and check subject? (2 way)

A

subject { ‘Hello’ }

it ‘checks subject’ do
expect(subject).to eq(‘Hello’)
is_expected.to eq(‘Hello’)
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to stub a specific date?

A

travel_to Date.parse(“2018-02-10”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to merge additional field to the hash inside context?

A
# 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are 3 ways to move common variables out of examples?

A
  1. before { @sandwich = Sandwich.new }
  2. let(:sandwich) { Sandwich.new }
  3. # ruby methoddef sandwich
    @sandwich ||= Sandwich.new
    end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to create pending specs?

A

Just add “it” without the block

it ‘works’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to run a hook only for the spec with metadata?

A

it ‘is delicious’ , sample: true

RSpec.configure do |config|
  config.after(:example, sample: true) do 
    puts 'sample'
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What hooks do you know?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly