class TestAutosaveAssociationOnABelongsToAssociation

Public Instance Methods

setup() click to toggle source
Calls superclass method
# File activerecord/test/cases/autosave_association_test.rb, line 1275
def setup
  super
  @ship = Ship.create(name: "Nights Dirty Lightning")
  @pirate = @ship.create_pirate(catchphrase: "Don' botharrr talkin' like one, savvy?")
end
test_should_automatically_save_bang_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1294
def test_should_automatically_save_bang_the_associated_model
  @ship.pirate.catchphrase = "Arr"
  @ship.save!
  assert_equal "Arr", @ship.reload.pirate.catchphrase
end
test_should_automatically_save_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1288
def test_should_automatically_save_the_associated_model
  @ship.pirate.catchphrase = "Arr"
  @ship.save
  assert_equal "Arr", @ship.reload.pirate.catchphrase
end
test_should_automatically_validate_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1300
def test_should_automatically_validate_the_associated_model
  @ship.pirate.catchphrase = ""
  assert @ship.invalid?
  assert @ship.errors[:"pirate.catchphrase"].any?
end
test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1306
def test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid
  @ship.name = nil
  @ship.pirate.catchphrase = nil
  assert @ship.invalid?
  assert @ship.errors[:name].any?
  assert @ship.errors[:"pirate.catchphrase"].any?
end
test_should_not_load_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1363
def test_should_not_load_the_associated_model
  assert_queries(1) { @ship.name = "The Vile Insanity"; @ship.save! }
end
test_should_not_save_and_return_false_if_a_callback_cancelled_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1333
def test_should_not_save_and_return_false_if_a_callback_cancelled_saving
  ship = Ship.new(name: "The Vile Insanity")
  pirate = ship.build_pirate(catchphrase: "Arr")
  pirate.cancel_save_from_callback = true

  assert_no_difference "Ship.count" do
    assert_no_difference "Pirate.count" do
      assert !ship.save
    end
  end
end
test_should_rollback_any_changes_if_an_exception_occurred_while_saving() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1345
def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
  before = [@ship.pirate.catchphrase, @ship.name]

  @ship.pirate.catchphrase = "Arr"
  @ship.name = "The Vile Insanity"

  # Stub the save method of the @ship.pirate instance to raise an exception
  class << @ship.pirate
    def save(*args)
      super
      raise "Oh noes!"
    end
  end

  assert_raise(RuntimeError) { assert !@ship.save }
  assert_equal before, [@ship.pirate.reload.catchphrase, @ship.reload.name]
end
test_should_still_allow_to_bypass_validations_on_the_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1314
def test_should_still_allow_to_bypass_validations_on_the_associated_model
  @ship.pirate.catchphrase = ""
  @ship.name = ""
  @ship.save(validate: false)
  # Oracle saves empty string as NULL
  if current_adapter?(:OracleAdapter)
    assert_equal [nil, nil], [@ship.reload.name, @ship.pirate.catchphrase]
  else
    assert_equal ["", ""], [@ship.reload.name, @ship.pirate.catchphrase]
  end
end
test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1326
def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
  @ship.pirate.catchphrase = ""
  assert_raise(ActiveRecord::RecordInvalid) do
    @ship.save!
  end
end
test_should_still_work_without_an_associated_model() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 1281
def test_should_still_work_without_an_associated_model
  @pirate.destroy
  @ship.reload.name = "The Vile Insanity"
  @ship.save
  assert_equal "The Vile Insanity", @ship.reload.name
end