aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/attribute_methods_test.rb
blob: 5659dcbc4803c151ac05afa6e798e8f221bce727 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'cases/helper'

class ModelWithAttributes
  include ActiveModel::AttributeMethods
  
  attribute_method_suffix ''

  def attributes
    { :foo => 'value of foo' }
  end

private
  def attribute(name)
    attributes[name.to_sym]
  end
end

class ModelWithAttributes2
  include ActiveModel::AttributeMethods
  
  attribute_method_suffix '_test'
end

class AttributeMethodsTest < ActiveModel::TestCase
  test 'unrelated classes should not share attribute method matchers' do
    assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
                     ModelWithAttributes2.send(:attribute_method_matchers)
  end

  test '#define_attribute_methods generates attribute methods' do
    ModelWithAttributes.define_attribute_methods([:foo])

    assert ModelWithAttributes.attribute_methods_generated?
    assert ModelWithAttributes.new.respond_to?(:foo)
    assert_equal "value of foo", ModelWithAttributes.new.foo
  end

  test '#undefine_attribute_methods removes attribute methods' do
    ModelWithAttributes.define_attribute_methods([:foo])
    ModelWithAttributes.undefine_attribute_methods

    assert !ModelWithAttributes.attribute_methods_generated?
    assert !ModelWithAttributes.new.respond_to?(:foo)
    assert_raises(NoMethodError) { ModelWithAttributes.new.foo }
  end
end