ruby on rails - How to test model's callback method independently? -
i had method in model:
class article < activerecord::base def do_something end end i had unit test method:
# spec/models/article_spec.rb describe "#do_something" @article = factorygirl.create(:article) "should work expected" @article.do_something expect(@article).to have_something end # ...several other examples different cases end everything fine until found it's better move method after_save callback:
class article < activerecord::base after_save :do_something def do_something end end now tests method broken. have fix by:
- no more specific call
do_somethingbecausecreateorsavetrigger method well, or i'll meet duplicate db actions. - change
createbuild - test respond_to
use general
model.saveinstead of individual method callmodel.do_somethingdescribe "#do_something" @article = factorygirl.build(:article) "should work expected" expect{@article.save}.not_to raise_error expect(@article).to have_something expect(@article).to respond_to(:do_something) end end
the test passed concern it's no longer specific method. effect mixed other callbacks if more added.
my question is, there beautiful way test model's instance methods independently becoming callback?
callback , callback behavior independent tests. if want check after_save callback, need think of 2 things:
- is callback being fired right events?
- is called function doing right thing?
assume have article class many callbacks, how test:
class article < activerecord::base after_save :do_something after_destroy :do_something_else ... end "triggers do_something on save" expect(@article).to receive(:do_something) @article.save end "triggers do_something_else on destroy" expect(@article).to receive(:do_something_else) @article.destroy end "#do_something should work expected" # actual tests do_something method end this decouples callbacks behavior. example, trigger same callback method article.do_something when other related object updated, user.before_save { user.article.do_something }. accomodate those.
so, keep testing methods usual. worry callbacks separately.
edit: typos , potential misconceptions edit: change "do something" "trigger something"
Comments
Post a Comment