~vcs-imports/rails/trunk

« back to all changes in this revision

Viewing changes to activerecord/test/cases/finder_respond_to_test.rb

  • Committer: pratik
  • Date: 2008-04-06 22:26:15 UTC
  • Revision ID: vcs-imports@canonical.com-20080406222615-zfonn64xy4ax94ev
Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper]

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require "cases/helper"
 
2
require 'models/topic'
 
3
 
 
4
class FinderRespondToTest < ActiveRecord::TestCase
 
5
 
 
6
  fixtures :topics
 
7
 
 
8
  def test_should_preserve_normal_respond_to_behaviour_and_respond_to_newly_added_method
 
9
    class << Topic; self; end.send(:define_method, :method_added_for_finder_respond_to_test) { }
 
10
    assert Topic.respond_to?(:method_added_for_finder_respond_to_test)
 
11
  ensure
 
12
    class << Topic; self; end.send(:remove_method, :method_added_for_finder_respond_to_test)
 
13
  end
 
14
 
 
15
  def test_should_preserve_normal_respond_to_behaviour_and_respond_to_standard_object_method
 
16
    assert Topic.respond_to?(:to_s)
 
17
  end
 
18
 
 
19
  def test_should_respond_to_find_by_one_attribute_before_caching
 
20
    ensure_topic_method_is_not_cached(:find_by_title)
 
21
    assert Topic.respond_to?(:find_by_title)
 
22
  end
 
23
 
 
24
  def test_should_respond_to_find_all_by_one_attribute
 
25
    ensure_topic_method_is_not_cached(:find_all_by_title)
 
26
    assert Topic.respond_to?(:find_all_by_title)
 
27
  end
 
28
 
 
29
  def test_should_respond_to_find_all_by_two_attributes
 
30
    ensure_topic_method_is_not_cached(:find_all_by_title_and_author_name)
 
31
    assert Topic.respond_to?(:find_all_by_title_and_author_name)
 
32
  end
 
33
 
 
34
  def test_should_respond_to_find_by_two_attributes
 
35
    ensure_topic_method_is_not_cached(:find_by_title_and_author_name)
 
36
    assert Topic.respond_to?(:find_by_title_and_author_name)
 
37
  end
 
38
 
 
39
  def test_should_respond_to_find_or_initialize_from_one_attribute
 
40
    ensure_topic_method_is_not_cached(:find_or_initialize_by_title)
 
41
    assert Topic.respond_to?(:find_or_initialize_by_title)
 
42
  end
 
43
 
 
44
  def test_should_respond_to_find_or_initialize_from_two_attributes
 
45
    ensure_topic_method_is_not_cached(:find_or_initialize_by_title_and_author_name)
 
46
    assert Topic.respond_to?(:find_or_initialize_by_title_and_author_name)
 
47
  end
 
48
 
 
49
  def test_should_respond_to_find_or_create_from_one_attribute
 
50
    ensure_topic_method_is_not_cached(:find_or_create_by_title)
 
51
    assert Topic.respond_to?(:find_or_create_by_title)
 
52
  end
 
53
 
 
54
  def test_should_respond_to_find_or_create_from_two_attributes
 
55
    ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name)
 
56
    assert Topic.respond_to?(:find_or_create_by_title_and_author_name)
 
57
  end
 
58
 
 
59
  def test_should_not_respond_to_find_by_one_missing_attribute
 
60
    assert !Topic.respond_to?(:find_by_undertitle)
 
61
  end
 
62
 
 
63
  def test_should_not_respond_to_find_by_invalid_method_syntax
 
64
    assert !Topic.respond_to?(:fail_to_find_by_title)
 
65
    assert !Topic.respond_to?(:find_by_title?)
 
66
    assert !Topic.respond_to?(:fail_to_find_or_create_by_title)
 
67
    assert !Topic.respond_to?(:find_or_create_by_title?)
 
68
  end
 
69
 
 
70
  private
 
71
 
 
72
  def ensure_topic_method_is_not_cached(method_id)
 
73
    class << Topic; self; end.send(:remove_method, method_id) if Topic.public_methods.any? { |m| m.to_s == method_id.to_s }
 
74
  end
 
75
 
 
76
end
 
 
b'\\ No newline at end of file'