Sha256: b4091163db6ecb7dbab84eb8bff6f86e8af5b2ecb8ba1e7770166a6e2a364511

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'test_helper'

class Superstore::AttributeMethodsTest < Superstore::TestCase
  test 'read and write attributes' do
    issue = Issue.new
    assert_nil issue.read_attribute(:description)

    issue.write_attribute(:description, nil)
    assert_nil issue.read_attribute(:description)

    issue.write_attribute(:description, 'foo')
    assert_equal 'foo', issue.read_attribute(:description)
  end

  test 'hash accessor aliases' do
    issue = Issue.new

    issue[:description] = 'bar'

    assert_equal 'bar', issue[:description]
  end

  test 'attributes setter' do
    issue = Issue.new

    issue.attributes = {
      description: 'foo'
    }

    assert_equal 'foo', issue.description
  end

  class ModelWithOverride < Superstore::Base
    attribute :title, type: :string

    def title=(v)
      super "#{v} lol"
    end
  end

   test 'override' do
    issue = ModelWithOverride.new(title: 'hey')

     assert_equal 'hey lol', issue.title
  end

  test 'has_attribute?' do
    refute Issue.new.has_attribute?(:unknown)
    assert Issue.new.has_attribute?(:description)
    assert Issue.new(description: nil).has_attribute?(:description)
    assert Issue.new(description: 'hey').has_attribute?(:description)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
superstore-3.0.0 test/unit/attribute_methods_test.rb
superstore-2.5.0 test/unit/attribute_methods_test.rb