2012-03-02 23:20:17 -05:00
|
|
|
require 'cases/helper'
|
|
|
|
|
|
|
|
class ModelTest < ActiveModel::TestCase
|
|
|
|
include ActiveModel::Lint::Tests
|
|
|
|
|
2013-06-18 21:21:35 -04:00
|
|
|
module DefaultValue
|
2013-06-19 09:32:05 -04:00
|
|
|
def self.included(klass)
|
|
|
|
klass.class_eval { attr_accessor :hello }
|
|
|
|
end
|
|
|
|
|
2013-06-18 21:21:35 -04:00
|
|
|
def initialize(*args)
|
|
|
|
@attr ||= 'default value'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-02 23:20:17 -05:00
|
|
|
class BasicModel
|
2013-06-18 21:21:35 -04:00
|
|
|
include DefaultValue
|
2012-03-02 23:20:17 -05:00
|
|
|
include ActiveModel::Model
|
|
|
|
attr_accessor :attr
|
|
|
|
end
|
|
|
|
|
2013-06-19 09:32:05 -04:00
|
|
|
class SimpleModel
|
|
|
|
include ActiveModel::Model
|
|
|
|
end
|
|
|
|
|
2012-03-02 23:20:17 -05:00
|
|
|
def setup
|
|
|
|
@model = BasicModel.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_with_params
|
2013-05-01 20:10:06 -04:00
|
|
|
object = BasicModel.new(attr: "value")
|
2013-06-19 09:32:05 -04:00
|
|
|
assert_equal "value", object.attr
|
2012-03-02 23:20:17 -05:00
|
|
|
end
|
2012-03-03 02:19:39 -05:00
|
|
|
|
|
|
|
def test_initialize_with_nil_or_empty_hash_params_does_not_explode
|
|
|
|
assert_nothing_raised do
|
|
|
|
BasicModel.new()
|
2013-01-12 14:31:33 -05:00
|
|
|
BasicModel.new nil
|
2012-03-03 02:19:39 -05:00
|
|
|
BasicModel.new({})
|
|
|
|
end
|
|
|
|
end
|
2013-01-12 14:31:33 -05:00
|
|
|
|
|
|
|
def test_persisted_is_always_false
|
2013-05-01 20:10:06 -04:00
|
|
|
object = BasicModel.new(attr: "value")
|
2013-01-12 14:31:33 -05:00
|
|
|
assert object.persisted? == false
|
|
|
|
end
|
2013-06-18 21:21:35 -04:00
|
|
|
|
|
|
|
def test_mixin_inclusion_chain
|
|
|
|
object = BasicModel.new
|
2013-06-19 09:32:05 -04:00
|
|
|
assert_equal 'default value', object.attr
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mixin_initializer_when_args_exist
|
|
|
|
object = BasicModel.new(hello: 'world')
|
|
|
|
assert_equal 'world', object.hello
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mixin_initializer_when_args_dont_exist
|
|
|
|
assert_raises(NoMethodError) { SimpleModel.new(hello: 'world') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mixin_when_no_ancestors
|
|
|
|
assert SimpleModel.new
|
2013-06-18 21:21:35 -04:00
|
|
|
end
|
2012-03-02 23:20:17 -05:00
|
|
|
end
|