Skip to content

Instantly share code, notes, and snippets.

@gingray
Last active August 29, 2015 14:24
Show Gist options
  • Save gingray/83e819dd3104183950f8 to your computer and use it in GitHub Desktop.
Save gingray/83e819dd3104183950f8 to your computer and use it in GitHub Desktop.
RSpec common practice and technique
#stub chain of methods
```
describe '#chain stub' do
subject { Object.new }
let(:stb) { double sync:calc }
before { allow(subject).to receive_message_chain('method1.mathod2.calc') { stb } }
before { subject.method1.mathod2.calc }
it { expect(stb).to have_received(:calc) }
end
```
#stub method
```
describe '#method stub' do
subject { Object.new }
let(:stb) { double sync:calc }
before { allow(subject).to receive :method1 }
before { subject.method1 }
it { expect(stb).to have_received(:method1) }
end
```
#stub method with argument check
```
is_expected.to have_received(:update).with(array_including([ad1, ad2]))
is_expected.to have_received(:update) { |arr| expect(arr.count).to eq 2 }
```
#count receiving messages
```
expect(...).to receive(...).once
expect(...).to receive(...).twice
expect(...).to receive(...).exactly(n).times
expect(...).to receive(...).at_least(:once)
expect(...).to receive(...).at_least(:twice)
expect(...).to receive(...).at_least(n).times
expect(...).to receive(...).at_most(:once)
expect(...).to receive(...).at_most(:twice)
expect(...).to receive(...).at_most(n).times
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment