RSpec double/stubs Flashcards

1
Q

How to stub value and return something?

A
before do
    # it calls stub 
    allow(animal).to receive(:name).and_return('hello') 
  end

it { expect(animal.name).to eq ‘hello’ }

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

How to mock values with expectations?

A
# it calls mock
expect(animal).to receive(:name).and_return('hello')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create spy?

A
allow(thing).to receive(:name).and_return("Fred")
# body of test
expect(thing).to have_received(:name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the full doubles?

A

Full double - entire objects that exist only to be

stubs

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

What is partial doubles?

A

Partial doubles - can stub specific methods of existing

objects.

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

How to create a full double?

A

let(:animal) { double(‘Optional name’, name: ‘hello’, age: 10) }

it { expect(animal.name).to eq ‘hello’ }

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

How to return double itself if double does implement this method? ( 2 methods)

A

1) spy(‘Name’)

2) double(‘Optional name’).as_null_object

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

What double types do you know? And what is the difference between them?

A
# allows any argument double('Project', name2: 'dd' )
1) double 
# allows to send only existing instance methods instance_double('Project', name: 'dd' )
2) instance_double 
# allows to send only existing class methods instance_double('Project', name: 'dd' )
3) class_double
# take an instance of obejct as the first argument object_double(Project.new, name: 'dd' )
4) object_double
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to stub any instance of an object?

A

allow_any_instance_of(Project).to receive(:name).and_return(false)

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

How to stub exception raising?

A

allow(animal).to receive(:name).and_raise(‘Error’)

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

How to check that we run an object method twice?

A

expect(proj).to receive(:name).twice

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

How to check that the object method doesn’t invoke?

A

expect(proj).not_to receive(:name)

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

How to stub method with argument?

A

allow(animal).to receive(:name).with(1)

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

How to stub method and call original?

A

allow(animal).to receive(:name).and_call_original

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

How to check that we invoke something with a hash?

A

expect(animal).to receive(:name).with(a_hash_including(name: ‘Sample’))

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

How to check that we invoke something with the specific argument type?

A

expect(animal).to receive(:name).with(a_kind_of(OpenStruct))

17
Q

How to check that we call some method twice in the cycle?

A

use and

expect { subject }.to publish(‘something’).with(
company: company1, employee_ids: employee_ids1).and publish(‘something’).with(
company: company2, employee_ids: employee_ids2
)

18
Q

What main stub/mock abstraction do you know? And what does they mean?

A
  • Doubles (which act as fake collaborators in tests)
    double(upvotes: 0, downvotes: 5)
  • Stubbing ( Sometimes however, we have to
    deal with collaborators that are hard-coded inside our object. We can isolate these objects too with a technique called stubbing.)
    allow(Link).to receive(:new).and_return(invalid_link)
  • Mocking (When mocking an interaction with a collaborator we set up an expectation that it will receive a given message and then exercise the system to see if that does indeed happen.)
    expect(LinkMailer).to receive(:new_link).with(valid_link)
  • Spying (Mocking can be a little weird because the expectation happens in the middle of the test. Spy checks expectation after the test was run)
    expect(LinkMailer).to have_received(:new_link).with(link)