RSpec: how to test if a method was called?

后端 未结 4 2222
眼角桃花
眼角桃花 2020-12-13 03:00

When writing RSpec tests, I find myself writing a lot of code that looks like this in order to ensure that a method was called during the execution of a test (for the sake o

4条回答
  •  不思量自难忘°
    2020-12-13 03:59

    The below should work

    describe "#foo"
      it "should call 'bar' with appropriate arguments" do
         subject.stub(:bar)
         subject.foo
         expect(subject).to have_received(:bar).with("Invalid number of arguments")
      end
    end
    

    Documentation: https://github.com/rspec/rspec-mocks#expecting-arguments

提交回复
热议问题