rspec receive counts

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

Read More

sick bullshit with message_chaining

describe '#method' do

it 'get branch name based on a repo and a pullrequest id' do
      pulls = [double({ 'head' => double({ 'ref' => 'number'})})]
      pr = pulls.first
      expect(connection.client).to receive(:pull_requests).with(connection.repo).and_return pulls
      expect(pulls).to receive(:[]).with(3).and_return pr
      expect(pr).to receive(:head).and_return pr.head
      connection.get_branch_name(3, connection.repo)
end
end

https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/matchers/receive_message_chain.rb

new: expect(object).to receive_chained_messages(:m1, :m2, :m3) or old..

Read More

rspec mocking with double and setting instance variables

Make sure to first set the doubles

trello = double(‘trello’)
card =  double(‘card’)
member = double(‘member’)

And then set the instance variables
subject.instance_variable_set(:@trello, trello)
subject.instance_variable_set(:@card, card)
expect(subject).to receive(:find_member).and_return member > stubs
expect(trello).to receive(:add_commander_to_card).with(member, card) => expects

subject.add_member_to_card => call

 

 

subject.instance_variable_set(:@card, double(‘card’))

Read More