Suppose I have the following ActiveRecord class:
class ToastMitten < ActiveRecord::Base
before_save :brush_off_crumbs
end
Is there a clean way to test that :brush_off_crumbs
has been set as a before_save
callback?
By "clean" I mean:
before_save
directive; I need to test that I correctly told it what to do before it saves.I found a way that satisfies criteria #1 but not #2:
it "should call have brush_off_crumbs as a before_save callback" do
# undocumented voodoo
before_save_callbacks = ToastMitten._save_callbacks.select do |callback|
callback.kind.eql?(:before)
end
# vile incantations
before_save_callbacks.map(&:raw_filter).should include(:brush_off_crumbs)
end
run_callbacks
This is less hacky, but not perfect:
it "is called as a before_save callback" do
revenue_object.should_receive(:record_financial_changes)
revenue_object.run_callbacks(:save) do
# Bail from the saving process, so we'll know that if the method was
# called, it was done before saving
false
end
end
Using this technique to test for an after_save
would be more awkward.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With