From 01a1be4438e55e93c199be9ec2cdd4857c520fff Mon Sep 17 00:00:00 2001 From: Ben Atkins Date: Thu, 7 Feb 2013 15:27:29 -0500 Subject: [PATCH] Added basic test coverage for a model that invokes for protection from mass assignment. --- test/dummy/app/models/protected_widget.rb | 3 ++ test/unit/protected_attrs_test.rb | 41 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 test/dummy/app/models/protected_widget.rb create mode 100644 test/unit/protected_attrs_test.rb diff --git a/test/dummy/app/models/protected_widget.rb b/test/dummy/app/models/protected_widget.rb new file mode 100644 index 00000000..51fbae8a --- /dev/null +++ b/test/dummy/app/models/protected_widget.rb @@ -0,0 +1,3 @@ +class ProtectedWidget < Widget + attr_accessible :name, :a_text +end diff --git a/test/unit/protected_attrs_test.rb b/test/unit/protected_attrs_test.rb new file mode 100644 index 00000000..69095bb3 --- /dev/null +++ b/test/unit/protected_attrs_test.rb @@ -0,0 +1,41 @@ +require 'test_helper' + +class ProtectedAttrsTest < ActiveSupport::TestCase + subject { ProtectedWidget.new } + + accessible_attrs = ProtectedWidget.accessible_attributes.to_a + accessible_attrs.each do |attr_name| + should allow_mass_assignment_of(attr_name.to_sym) + end + ProtectedWidget.column_names.reject { |column_name| accessible_attrs.include?(column_name) }.each do |attr_name| + should_not allow_mass_assignment_of(attr_name.to_sym) + end + + context 'A model with `attr_accessible` created' do + setup do + @widget = ProtectedWidget.create! :name => 'Henry' + @initial_attributes = @widget.attributes + end + + should 'be `nil` in its previous version' do + assert_nil @widget.previous_version + end + + context 'which is then updated' do + setup do + @widget.assign_attributes(:name => 'Jeff', :a_text => 'Short statement') + @widget.an_integer = 42 + @widget.save! + end + + should 'not be `nil` in its previous version' do + assert_not_nil @widget.previous_version + end + + should 'the previous version should contain right attributes' do + assert_equal @widget.previous_version.attributes, @initial_attributes + end + end + + end +end