Check method call on model using MiniTest

前端 未结 2 1127
故里飘歌
故里飘歌 2020-12-21 01:58

If I was using RSpec I could test if a method is being called like so:

expect(obj).to receive(:method)

What is the equivalent in MiniTest?

2条回答
  •  既然无缘
    2020-12-21 02:40

    For this purpose Minitest has a .expect :call which allows you to check if method is getting called:

    describe Post do
    
      before do
        @post = FactoryGirl.build(:post)
      end
    
      describe "when saving" do
        it "calls the create_slug method before validation" do
          mock_method = MiniTest::Mock.new
          mock_method.expect :call, "return_value", []
          @post.stub :create_slug, mock_method do
            @post.save
          end
          mock_method.verify
        end
      end
    
    end
    

    If @post.create_slug was called, test will pass. Otherwise test will raise a MockExpectationError.

    Unfortunately this feature is not documented very well. I found this answer from here: https://github.com/seattlerb/minitest/issues/216

提交回复
热议问题