Permalink
Browse files

Adding should have_before_save_callback(method) and friends.

  • Loading branch information...
1 parent ef3863e commit 6dc34f6042271a4f66b86d0b387652592abe0e7d @jqr jqr committed with Jan 15, 2009
@@ -1,5 +1,5 @@
require 'remarkable/active_record/helpers'
-%w( database associations validations ).each do |folder|
+%w( database associations validations callbacks ).each do |folder|
Dir[File.join(File.dirname(__FILE__), "macros", folder, '*.rb')].each do |file|
require file
end
@@ -0,0 +1,49 @@
+module Remarkable # :nodoc:
+ module ActiveRecord # :nodoc:
+ module Matchers # :nodoc:
+
+ ::ActiveRecord::Callbacks::CALLBACKS.each do |callback|
+ define_method("have_#{callback}_callback") do |method|
+ CallbackMatcher.new(callback, method)
+ end
+ end
+
+ class CallbackMatcher < Remarkable::Matcher::Base
+ def initialize(callback, method)
+ @callback = callback
+ @method = method
+ end
+
+ def matches?(subject)
+ @subject = subject
+
+ assert_matcher_for(@callback) do |column|
+ callbacks_for(@callback).include?(@method)
+ end
+ end
+
+ def failure_message
+ "Expected #{expectation}"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
+
+ def description
+ "have a #{@callback} callback named #{@method}"
+ end
+
+ protected
+
+ def expectation
+ "#{model_name} to #{description}"
+ end
+
+ def callbacks_for(callback)
+ model_class.send("#{callback}_callback_chain").collect(&:method)
+ end
+ end
+ end
+ end
+end
@@ -40,6 +40,11 @@
it { should_not have_named_scope("recent(#{@count})", :limit => 1) }
end
+ it { should have_after_create_callback(:send_welcome_email) }
+ it { should_not have_after_save_callback(:send_welcome_email) }
+ it { should_not have_after_destroy_callback(:goodbye_jerk) }
+ it { should have_before_validation_on_update_callback(:some_weird_callback) }
+
it { should_not allow_values_for(:email, "blah", "b lah") }
it { should allow_values_for(:email, "a@b.com", "asdf@asdf.com") }
it { should_not allow_values_for(:email, "a@b.com", "asdf@asdf.com").allow_nil }
@@ -152,6 +157,11 @@
end
# should_have_named_scope "recent(@count)", :limit => 2
end
+
+ should_have_after_create_callback(:send_welcome_email)
+ should_not_have_after_save_callback(:send_welcome_email)
+ should_not_have_after_destroy_callback(:goodbye_jerk)
+ should_have_before_validation_on_update_callback(:some_weird_callback)
should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
@@ -25,4 +25,10 @@ def self.recent_via_method(count)
validates_uniqueness_of :email, :scope => :name
validates_length_of :ssn, :is => 9, :message => "Social Security Number is not the right length"
validates_numericality_of :ssn
+
+ after_create :send_welcome_email
+ before_validation_on_update :some_weird_callback
+
+ def send_welcome_email; end
+ def some_weird_callback; end
end

0 comments on commit 6dc34f6

Please sign in to comment.