How to test model's callback method independently?

后端 未结 6 1125
感情败类
感情败类 2021-02-02 07:34

I had a method in a model:

class Article < ActiveRecord::Base
  def do_something
  end
end

I also had a unit test for this method:



        
6条回答
  •  情歌与酒
    2021-02-02 07:59

    You can use shoulda-callback-matchers to test existence of your callbacks without calling them.

    describe Article do
      it { is_expected.to callback(:do_something).after(:save) }
    end
    

    If you also want to test the behaviour of the callback:

    describe Article do
      ...
    
      describe "#do_something" do
        it "gives the article something" do
          @article.save
          expect(@article).to have_something
        end
      end
    end
    

提交回复
热议问题