Sha256: f985e1986a192a52e03758e9336be09963fe2cf127c3415c6e2b38d9d04730b8

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'test/unit'
require 'rubygems'
require 'activerecord'
require File.join(File.dirname(__FILE__), '..', 'lib', 'immutable_attributes')

ActiveRecord::Base.establish_connection(
  :adapter  => "sqlite3",
  :database => ":memory:"
)
ActiveRecord::Schema.define do
  create_table :records do |table|
    table.column :name, :string
    table.column :body, :string
  end
end

class Record < ActiveRecord::Base
  attr_immutable :name
end

class ImmutableAttributesTest < Test::Unit::TestCase

  def test_immutable_attribute_can_be_set
    assert Record.new(:name => 'record name')
  end

  def test_immutable_attribute_cannot_be_changed_via_mass_setter
    record = Record.create!(:name => 'record name')
    assert_raises(ActiveRecord::ImmutableAttributeError) { record.update_attributes(:name => 'new name') }
  end

  def test_immutable_attribute_cannot_be_changed_via_bracket_setter
    record = Record.create!(:name => 'record name')
    assert_raises(ActiveRecord::ImmutableAttributeError) { record[:name] = 'new name' }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
immutable_attributes-1.2.0 test/immutable_attributes_test.rb