2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2012-03-02 23:20:17 -05:00
|
|
|
|
2019-08-01 17:18:19 -04:00
|
|
|
class ModelTest < ActiveSupport::TestCase
|
2012-03-02 23:20:17 -05:00
|
|
|
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)
|
2016-08-06 12:38:23 -04:00
|
|
|
@attr ||= "default value"
|
2013-06-19 22:16:17 -04:00
|
|
|
super
|
2013-06-18 21:21:35 -04:00
|
|
|
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 22:16:17 -04:00
|
|
|
class BasicModelWithReversedMixins
|
|
|
|
include ActiveModel::Model
|
|
|
|
include DefaultValue
|
|
|
|
attr_accessor :attr
|
|
|
|
end
|
|
|
|
|
2013-06-19 09:32:05 -04:00
|
|
|
class SimpleModel
|
|
|
|
include ActiveModel::Model
|
2013-06-19 22:16:17 -04:00
|
|
|
attr_accessor :attr
|
2013-06-19 09:32:05 -04:00
|
|
|
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
|
|
|
|
2013-06-19 22:16:17 -04:00
|
|
|
def test_initialize_with_params_and_mixins_reversed
|
|
|
|
object = BasicModelWithReversedMixins.new(attr: "value")
|
|
|
|
assert_equal "value", object.attr
|
|
|
|
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-06-19 22:16:17 -04:00
|
|
|
BasicModel.new(nil)
|
2012-03-03 02:19:39 -05:00
|
|
|
BasicModel.new({})
|
2016-08-06 12:38:23 -04:00
|
|
|
SimpleModel.new(attr: "value")
|
2012-03-03 02:19:39 -05:00
|
|
|
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
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "default value", object.attr
|
2013-06-19 09:32:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mixin_initializer_when_args_exist
|
2016-08-06 12:38:23 -04:00
|
|
|
object = BasicModel.new(hello: "world")
|
|
|
|
assert_equal "world", object.hello
|
2013-06-19 09:32:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mixin_initializer_when_args_dont_exist
|
2015-02-25 09:14:43 -05:00
|
|
|
assert_raises(ActiveModel::UnknownAttributeError) do
|
2016-08-06 12:38:23 -04:00
|
|
|
SimpleModel.new(hello: "world")
|
2015-01-23 17:36:43 -05:00
|
|
|
end
|
2013-06-19 09:32:05 -04:00
|
|
|
end
|
2012-03-02 23:20:17 -05:00
|
|
|
end
|