Skip to content

Commit 8402488

Browse files
Neeraj Singhjosevalim
authored andcommitted
adding new test for ActiveModel::Serialization
Signed-off-by: José Valim <jose.valim@gmail.com>
1 parent 94b850c commit 8402488

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "cases/helper"
2+
3+
class SerializationTest < ActiveModel::TestCase
4+
class User
5+
include ActiveModel::Serialization
6+
7+
attr_accessor :name, :email, :gender
8+
9+
def attributes
10+
@attributes ||= {'name' => 'nil', 'email' => 'nil', 'gender' => 'nil'}
11+
end
12+
13+
def foo
14+
'i_am_foo'
15+
end
16+
end
17+
18+
setup do
19+
@user = User.new
20+
@user.name = 'David'
21+
@user.email = 'david@example.com'
22+
@user.gender = 'male'
23+
end
24+
25+
def test_method_serializable_hash_should_work
26+
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
27+
assert_equal expected , @user.serializable_hash
28+
end
29+
30+
def test_method_serializable_hash_should_work_with_only_option
31+
expected = {"name"=>"David"}
32+
assert_equal expected , @user.serializable_hash(:only => [:name])
33+
end
34+
35+
def test_method_serializable_hash_should_work_with_except_option
36+
expected = {"gender"=>"male", "email"=>"david@example.com"}
37+
assert_equal expected , @user.serializable_hash(:except => [:name])
38+
end
39+
40+
def test_method_serializable_hash_should_work_with_methods_option
41+
expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"}
42+
assert_equal expected , @user.serializable_hash(:methods => [:foo])
43+
end
44+
45+
end

0 commit comments

Comments
 (0)