aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/finder_respond_to_test.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
commit4f39382a2fad5e299fce73ae0ba60f28dfddb21a (patch)
tree6b035a7f46206219bb0221917647f6182fe930b3 /activerecord/test/cases/finder_respond_to_test.rb
parent917423d664038d6791738a73ad1446437dbb71df (diff)
downloadrails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.gz
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.bz2
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.zip
Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/cases/finder_respond_to_test.rb')
-rw-r--r--activerecord/test/cases/finder_respond_to_test.rb76
1 files changed, 76 insertions, 0 deletions
diff --git a/activerecord/test/cases/finder_respond_to_test.rb b/activerecord/test/cases/finder_respond_to_test.rb
new file mode 100644
index 0000000000..4e6fecf11a
--- /dev/null
+++ b/activerecord/test/cases/finder_respond_to_test.rb
@@ -0,0 +1,76 @@
+require "cases/helper"
+require 'models/topic'
+
+class FinderRespondToTest < ActiveRecord::TestCase
+
+ fixtures :topics
+
+ def test_should_preserve_normal_respond_to_behaviour_and_respond_to_newly_added_method
+ class << Topic; self; end.send(:define_method, :method_added_for_finder_respond_to_test) { }
+ assert Topic.respond_to?(:method_added_for_finder_respond_to_test)
+ ensure
+ class << Topic; self; end.send(:remove_method, :method_added_for_finder_respond_to_test)
+ end
+
+ def test_should_preserve_normal_respond_to_behaviour_and_respond_to_standard_object_method
+ assert Topic.respond_to?(:to_s)
+ end
+
+ def test_should_respond_to_find_by_one_attribute_before_caching
+ ensure_topic_method_is_not_cached(:find_by_title)
+ assert Topic.respond_to?(:find_by_title)
+ end
+
+ def test_should_respond_to_find_all_by_one_attribute
+ ensure_topic_method_is_not_cached(:find_all_by_title)
+ assert Topic.respond_to?(:find_all_by_title)
+ end
+
+ def test_should_respond_to_find_all_by_two_attributes
+ ensure_topic_method_is_not_cached(:find_all_by_title_and_author_name)
+ assert Topic.respond_to?(:find_all_by_title_and_author_name)
+ end
+
+ def test_should_respond_to_find_by_two_attributes
+ ensure_topic_method_is_not_cached(:find_by_title_and_author_name)
+ assert Topic.respond_to?(:find_by_title_and_author_name)
+ end
+
+ def test_should_respond_to_find_or_initialize_from_one_attribute
+ ensure_topic_method_is_not_cached(:find_or_initialize_by_title)
+ assert Topic.respond_to?(:find_or_initialize_by_title)
+ end
+
+ def test_should_respond_to_find_or_initialize_from_two_attributes
+ ensure_topic_method_is_not_cached(:find_or_initialize_by_title_and_author_name)
+ assert Topic.respond_to?(:find_or_initialize_by_title_and_author_name)
+ end
+
+ def test_should_respond_to_find_or_create_from_one_attribute
+ ensure_topic_method_is_not_cached(:find_or_create_by_title)
+ assert Topic.respond_to?(:find_or_create_by_title)
+ end
+
+ def test_should_respond_to_find_or_create_from_two_attributes
+ ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name)
+ assert Topic.respond_to?(:find_or_create_by_title_and_author_name)
+ end
+
+ def test_should_not_respond_to_find_by_one_missing_attribute
+ assert !Topic.respond_to?(:find_by_undertitle)
+ end
+
+ def test_should_not_respond_to_find_by_invalid_method_syntax
+ assert !Topic.respond_to?(:fail_to_find_by_title)
+ assert !Topic.respond_to?(:find_by_title?)
+ assert !Topic.respond_to?(:fail_to_find_or_create_by_title)
+ assert !Topic.respond_to?(:find_or_create_by_title?)
+ end
+
+ private
+
+ def ensure_topic_method_is_not_cached(method_id)
+ class << Topic; self; end.send(:remove_method, method_id) if Topic.public_methods.any? { |m| m.to_s == method_id.to_s }
+ end
+
+end \ No newline at end of file