public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Fixed should_have_many regression [#60]

 * Removed the check for the :source option on the assocation as it did not 
 always yield the correct class name
 * Added tests to prevent the regression again
Ryan McGeary (author)
Sun Aug 31 09:14:16 -0700 2008
commit  68e42610bed1a1caf0a2bb5fe73f72ccd5e44e89
tree    d4f6a49d7569af4b0763a1ea1a46129fe6398f1c
parent  5411ccf9765ac517fb5079818e84304be2d96b94
...
20
21
22
23
 
24
25
26
...
395
396
397
398
399
400
401
402
403
404
405
...
409
410
411
412
 
413
414
415
...
422
423
424
 
 
 
425
426
427
...
20
21
22
 
23
24
25
26
...
395
396
397
 
 
 
 
 
398
399
400
...
404
405
406
 
407
408
409
410
...
417
418
419
420
421
422
423
424
425
0
@@ -20,7 +20,7 @@ module ThoughtBot # :nodoc:
0
     # For all of these helpers, the last parameter may be a hash of options.
0
     #
0
     module ActiveRecord
0
-      # Ensures that the model cannot be saved if one of the attributes listed is not present.  
0
+      # Ensures that the model cannot be saved if one of the attributes listed is not present.
0
       #
0
       # If an instance variable has been created in the setup named after the
0
       # model being tested, then this method will use that.  Otherwise, it will
0
@@ -395,11 +395,6 @@ module ThoughtBot # :nodoc:
0
             assert reflection, "#{klass.name} does not have any relationship to #{association}"
0
             assert_equal :has_many, reflection.macro
0
 
0
-            associated_klass_name = reflection.options[:class_name]
0
-            associated_klass_name = reflection.options[:source].to_s.classify if associated_klass_name.blank?
0
-            associated_klass_name = association.to_s.classify                 if associated_klass_name.blank?
0
-            associated_klass = associated_klass_name.constantize
0
-
0
             if through
0
               through_reflection = klass.reflect_on_association(through)
0
               assert through_reflection, "#{klass.name} does not have any relationship to #{through}"
0
@@ -409,7 +404,7 @@ module ThoughtBot # :nodoc:
0
             if dependent
0
               assert_equal dependent.to_s,
0
                            reflection.options[:dependent].to_s,
0
-                           "#{associated_klass.name} should have #{dependent} dependency"
0
+                           "#{association} should have #{dependent} dependency"
0
             end
0
 
0
             # Check for the existence of the foreign key on the other table
0
@@ -422,6 +417,9 @@ module ThoughtBot # :nodoc:
0
                 fk = reflection.primary_key_name
0
               end
0
 
0
+              associated_klass_name = (reflection.options[:class_name] || association.to_s.classify)
0
+              associated_klass = associated_klass_name.constantize
0
+
0
               assert associated_klass.column_names.include?(fk.to_s),
0
                      "#{associated_klass.name} does not have a #{fk} foreign key."
0
             end
...
2
3
4
 
 
 
5
6
7
8
9
10
 
11
12
13
14
 
15
16
17
...
2
3
4
5
6
7
8
9
10
11
12
 
13
14
15
16
 
17
18
19
20
0
@@ -2,16 +2,19 @@ class User < ActiveRecord::Base
0
   has_many :posts
0
   has_many :dogs, :foreign_key => :owner_id
0
 
0
+  has_many :friendships
0
+  has_many :friends, :through => :friendships
0
+
0
   has_one :address, :as => :addressable
0
 
0
   named_scope :old,      :conditions => "age > 50"
0
   named_scope :eighteen, :conditions => { :age => 18 }
0
   named_scope :recent,   lambda {|count| { :limit => count } }
0
-  
0
+
0
   def self.recent_via_method(count)
0
     scoped(:limit => count)
0
   end
0
-  
0
+
0
   attr_protected :password
0
   attr_readonly :name
0
 
...
6
7
8
 
 
 
9
10
11
...
13
14
15
16
 
17
18
19
...
32
33
34
35
 
36
37
38
39
 
40
41
42
...
6
7
8
9
10
11
12
13
14
...
16
17
18
 
19
20
21
22
...
35
36
37
 
38
39
40
41
 
42
43
44
45
0
@@ -6,6 +6,9 @@ class UserTest < Test::Unit::TestCase
0
   should_have_many :posts
0
   should_have_many :dogs
0
 
0
+  should_have_many :friendships
0
+  should_have_many :friends
0
+
0
   should_have_one :address
0
 
0
   should_have_indices :email, :name, [:email, :name]
0
@@ -13,7 +16,7 @@ class UserTest < Test::Unit::TestCase
0
 
0
   should_have_named_scope :old,       :conditions => "age > 50"
0
   should_have_named_scope :eighteen,  :conditions => { :age => 18 }
0
-  
0
+
0
   should_have_named_scope 'recent(5)',            :limit => 5
0
   should_have_named_scope 'recent(1)',            :limit => 1
0
   should_have_named_scope 'recent_via_method(7)', :limit => 7
0
@@ -32,11 +35,11 @@ class UserTest < Test::Unit::TestCase
0
   should_have_instance_methods :email, :age, :email=, :valid?
0
   should_have_db_columns :name, :email, :age
0
   should_have_db_column :id, :type => "integer", :primary => true
0
-  should_have_db_column :email, :type => "string", :default => nil,   :precision => nil, :limit    => 255, 
0
+  should_have_db_column :email, :type => "string", :default => nil,   :precision => nil, :limit    => 255,
0
                                 :null => true,     :primary => false, :scale     => nil, :sql_type => 'varchar(255)'
0
   should_require_acceptance_of :eula
0
   should_require_unique_attributes :email, :scoped_to => :name
0
-  
0
+
0
   should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length"
0
   should_only_allow_numeric_values_for :ssn
0
 

Comments