Permalink
Browse files

For #3045

  • Loading branch information...
1 parent 3981310 commit 4c7d3a70b69569c7cd1fb24653373c22833f02c7 @alloy committed Jan 4, 2010
Showing with 37 additions and 6 deletions.
  1. +18 −0 app/models/task.rb
  2. +19 −6 test/unit/project_test.rb
View
@@ -1,2 +1,20 @@
class Task < ActiveRecord::Base
+ attr_accessor :fail
+ belongs_to :project
+
+ before_save :cause_before_save_problems
+ after_save :cause_after_save_problems
+ validate :cause_validation_problems
+
+ def cause_validation_problems
+ errors.add(:fail, 'in validation') if fail == :in_validation
+ end
+
+ def cause_before_save_problems
+ return false if fail == :before_save
+ end
+
+ def cause_after_save_problems
+ raise ActiveRecord::Rollback if fail == :after_save
+ end
end
View
@@ -1,13 +1,26 @@
require 'test_helper'
class ProjectTest < ActiveSupport::TestCase
- def test_case_name
-project = Project.create! :owner_attributes => { :name => 'Bello' }
+ self.use_transactional_fixtures = false
-# without attr_accessible
-p project.owner # => #<Author id: 980190963, name: "Bello", project_id: 980190963, owner: true, admin: true>
+ def test_project_rolls_back_when_task_validation_fails
+ @project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :in_validation } }
-# with: attr_accessible :name
-p project.owner # => #<Author id: 980190963, name: "Bello", project_id: 980190963, owner: nil, admin: nil>
+ assert @project.tasks.first.new_record?
+ assert @project.new_record?
+ end
+
+ def test_project_rolls_back_when_task_before_save_fails
+ @project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :before_save } }
+
+ assert @project.tasks.first.new_record?
+ assert @project.new_record?
+ end
+
+ def test_project_rolls_back_when_task_after_save_fails
+ @project = Project.create :name => 'Foo', :tasks_attributes => { '0' => { :name => 'Bar', :fail => :after_save } }
+
+ assert @project.tasks.first.new_record?
+ assert @project.new_record?
end
end

0 comments on commit 4c7d3a7

Please sign in to comment.