Browse files

This test does not test anything that happens in the real world. If you

recreate the models without mucking with internal caches of the relation
objects, then the test fails.

For example:

class Man < ActiveRecord::Base
  has_many :interests
end

class Interest < ActiveRecord::Base
  belongs_to :man
end

Then do this test:

def test_validate_presence_of_parent_fails_without_inverse_of
  repair_validations(Interest) do
    Interest.validates_presence_of(:man)
    assert_no_difference ['Man.count', 'Interest.count'] do
      man = Man.create(:name => 'John',
                       :interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
      assert_not_predicate man.errors[:"interests.man"], :empty?
    end
  end
end

The test will fail.  This is a bad test, so I am removing it.
  • Loading branch information...
1 parent 934369f commit 8f37ba81abbd13b935ce0f26574ff2b720b552f1 @tenderlove tenderlove committed Jun 13, 2013
Showing with 0 additions and 27 deletions.
  1. +0 −7 activerecord/lib/active_record/reflection.rb
  2. +0 −20 activerecord/test/cases/nested_attributes_test.rb
View
7 activerecord/lib/active_record/reflection.rb
@@ -310,13 +310,6 @@ def inverse_of
@inverse_of ||= klass.reflect_on_association inverse_name
end
- # Clears the cached value of +@inverse_of+ on this object. This will
- # not remove the :inverse_of option however, so future calls on the
- # +inverse_of+ will have to recompute the inverse.
- def clear_inverse_of_cache!
- @inverse_of = nil
- end
-
def polymorphic_inverse_of(associated_class)
if has_inverse?
if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
View
20 activerecord/test/cases/nested_attributes_test.rb
@@ -797,26 +797,6 @@ def test_validate_presence_of_parent_works_with_inverse_of
end
end
- def test_validate_presence_of_parent_fails_without_inverse_of
- Man.accepts_nested_attributes_for(:interests)
- Man.reflect_on_association(:interests).options.delete(:inverse_of)
- Man.reflect_on_association(:interests).clear_inverse_of_cache!
- Interest.reflect_on_association(:man).options.delete(:inverse_of)
- Interest.reflect_on_association(:man).clear_inverse_of_cache!
-
- repair_validations(Interest) do
- Interest.validates_presence_of(:man)
- assert_no_difference ['Man.count', 'Interest.count'] do
- man = Man.create(:name => 'John',
- :interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
- assert_not_predicate man.errors[:"interests.man"], :empty?
- end
- end
- ensure
- Man.reflect_on_association(:interests).options[:inverse_of] = :man
- Interest.reflect_on_association(:man).options[:inverse_of] = :interests
- end
-
def test_can_use_symbols_as_object_identifier
@pirate.attributes = { :parrots_attributes => { :foo => { :name => 'Lovely Day' }, :bar => { :name => 'Blown Away' } } }
assert_nothing_raised(NoMethodError) { @pirate.save! }

0 comments on commit 8f37ba8

Please sign in to comment.