What is double method in rspec for?

前端 未结 2 505
花落未央
花落未央 2021-01-31 03:03

It is stated in rspec doc that I should use double method in order to create test double. But I can see that it works perfectly ok even if I don\'t use double

2条回答
  •  萌比男神i
    2021-01-31 03:37

    With RSpec Mocks 3.0 the behaviour of doubles has changed. You now may verify doubles, which means "RSpec will check that the methods being stubbed are actually present on the underlying object if it is available", but "no checking will happen if the underlying object or class is not defined".

    Verifying doubles requests you to be specific about the double type (instance, class, object, dynamic class, partial). Here is an example from the RSpec Relish for an instance double:

    RSpec.describe User, '#suspend!' do
      it 'notifies the console' do
        notifier = instance_double("ConsoleNotifier")
    
        expect(notifier).to receive(:notify).with("suspended as")
    
        user = User.new(notifier)
        user.suspend!
      end
    end
    

提交回复
热议问题