Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for should_have_one :through [#115 state:resolved]

  • Loading branch information...
commit 79c7b87b821a7cf3bb1f9465db6e5532e7d96399 1 parent f03757e
Joe Ferris jferris authored
4 lib/shoulda/active_record/macros.rb
View
@@ -349,10 +349,10 @@ def should_have_many(*associations)
# should_have_one :god # unless hindu
#
def should_have_one(*associations)
- dependent = get_options!(associations, :dependent)
+ dependent, through = get_options!(associations, :dependent, :through)
klass = model_class
associations.each do |association|
- matcher = have_one(association).dependent(dependent)
+ matcher = have_one(association).dependent(dependent).through(through)
should matcher.description do
assert_accepts(matcher, klass.new)
end
6 lib/shoulda/active_record/matchers/association_matcher.rb
View
@@ -133,7 +133,7 @@ def through_association_valid?
def through_association_exists?
if through_reflection.nil?
- "#{model_class.name} does not have any relationship to #{@through}"
+ @missing = "#{model_class.name} does not have any relationship to #{@through}"
false
else
true
@@ -142,10 +142,10 @@ def through_association_exists?
def through_association_correct?
if @through == reflection.options[:through]
- "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
- " but got it through #{reflection.options[:through]}"
true
else
+ @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
+ "but got it through #{reflection.options[:through]}"
false
end
end
9 test/matchers/active_record/association_matcher_test.rb
View
@@ -126,7 +126,9 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
define_model :parent do
has_many :children
end
- assert_rejects @matcher.through(:conceptions), Parent.new
+ assert_rejects @matcher.through(:conceptions),
+ Parent.new,
+ :message => /does not have any relationship to conceptions/
end
should "reject an association that has the wrong :through option" do
@@ -137,9 +139,12 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end
define_model :parent do
has_many :conceptions
+ has_many :relationships
has_many :children, :through => :conceptions
end
- assert_rejects @matcher.through(:relationships), Parent.new
+ assert_rejects @matcher.through(:relationships),
+ Parent.new,
+ :message => /through relationships, but got it through conceptions/
end
should "accept an association with a valid :dependent option" do
2  test/rails_root/app/models/profile.rb
View
@@ -0,0 +1,2 @@
+class Profile < ActiveRecord::Base
+end
2  test/rails_root/app/models/registration.rb
View
@@ -0,0 +1,2 @@
+class Registration < ActiveRecord::Base
+end
2  test/rails_root/app/models/user.rb
View
@@ -6,6 +6,8 @@ class User < ActiveRecord::Base
has_many :friends, :through => :friendships
has_one :address, :as => :addressable, :dependent => :destroy
+ has_one :registration
+ has_one :profile, :through => :registration
named_scope :old, :conditions => "age > 50"
named_scope :eighteen, :conditions => { :age => 18 }
12 test/rails_root/db/migrate/20090506203502_create_profiles.rb
View
@@ -0,0 +1,12 @@
+class CreateProfiles < ActiveRecord::Migration
+ def self.up
+ create_table :profiles do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :profiles
+ end
+end
14 test/rails_root/db/migrate/20090506203536_create_registrations.rb
View
@@ -0,0 +1,14 @@
+class CreateRegistrations < ActiveRecord::Migration
+ def self.up
+ create_table :registrations do |t|
+ t.integer :user_id
+ t.integer :profile_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :registrations
+ end
+end
7 test/unit/user_test.rb
View
@@ -66,4 +66,11 @@ class UserTest < ActiveSupport::TestCase
should_fail do
should_not_allow_mass_assignment_of :name, :age
end
+
+ should_have_one :profile, :through => :registration
+
+ should_fail do
+ should_have_one :profile, :through => :interview
+ should_have_one :address, :through => :registration
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.