Skip to content

Commit 24980fb

Browse files
Use actual object instead of Minitest::Mock
Using a `Minitest::Mock` makes these tests brittle. Therefore, use an actual object instead.
1 parent 9aecf3c commit 24980fb

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

activemodel/test/cases/attribute_test.rb

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,101 @@
44

55
module ActiveModel
66
class AttributeTest < ActiveModel::TestCase
7-
setup do
8-
@type = Minitest::Mock.new
7+
class InscribingType
8+
def cast(value)
9+
"cast(#{value})"
10+
end
11+
12+
def serialize(value)
13+
"serialize(#{value})"
14+
end
15+
16+
def deserialize(value)
17+
"deserialize(#{value})"
18+
end
919
end
1020

11-
teardown do
12-
assert @type.verify
21+
setup do
22+
@type = InscribingType.new
1323
end
1424

1525
test "from_database + read type casts from database" do
16-
@type.expect(:deserialize, "type cast from database", ["a value"])
1726
attribute = Attribute.from_database(nil, "a value", @type)
1827

19-
type_cast_value = attribute.value
20-
21-
assert_equal "type cast from database", type_cast_value
28+
assert_equal "deserialize(a value)", attribute.value
2229
end
2330

2431
test "from_user + read type casts from user" do
25-
@type.expect(:cast, "type cast from user", ["a value"])
2632
attribute = Attribute.from_user(nil, "a value", @type)
2733

28-
type_cast_value = attribute.value
29-
30-
assert_equal "type cast from user", type_cast_value
34+
assert_equal "cast(a value)", attribute.value
3135
end
3236

3337
test "reading memoizes the value" do
34-
@type.expect(:deserialize, "from the database", ["whatever"])
35-
attribute = Attribute.from_database(nil, "whatever", @type)
38+
count = 0
39+
@type.define_singleton_method(:deserialize) do |value|
40+
count += 1
41+
value
42+
end
3643

37-
type_cast_value = attribute.value
38-
second_read = attribute.value
44+
attribute = Attribute.from_database(nil, "whatever", @type)
3945

40-
assert_equal "from the database", type_cast_value
41-
assert_same type_cast_value, second_read
46+
attribute.value
47+
attribute.value
48+
assert_equal 1, count
4249
end
4350

4451
test "reading memoizes falsy values" do
45-
@type.expect(:deserialize, false, ["whatever"])
52+
count = 0
53+
@type.define_singleton_method(:deserialize) do |value|
54+
count += 1
55+
false
56+
end
57+
4658
attribute = Attribute.from_database(nil, "whatever", @type)
4759

4860
attribute.value
4961
attribute.value
62+
assert_equal 1, count
5063
end
5164

52-
test "read_before_typecast returns the given value" do
65+
test "value_before_type_cast returns the given value" do
5366
attribute = Attribute.from_database(nil, "raw value", @type)
5467

5568
raw_value = attribute.value_before_type_cast
5669

5770
assert_equal "raw value", raw_value
5871
end
5972

60-
test "from_database + read_for_database type casts to and from database" do
61-
@type.expect(:deserialize, "read from database", ["whatever"])
62-
@type.expect(:serialize, "ready for database", ["read from database"])
73+
test "from_database + value_for_database type casts to and from database" do
6374
attribute = Attribute.from_database(nil, "whatever", @type)
6475

65-
serialize = attribute.value_for_database
66-
67-
assert_equal "ready for database", serialize
76+
assert_equal "serialize(deserialize(whatever))", attribute.value_for_database
6877
end
6978

70-
test "from_user + read_for_database type casts from the user to the database" do
71-
@type.expect(:cast, "read from user", ["whatever"])
72-
@type.expect(:serialize, "ready for database", ["read from user"])
79+
test "from_user + value_for_database type casts from the user to the database" do
7380
attribute = Attribute.from_user(nil, "whatever", @type)
7481

75-
serialize = attribute.value_for_database
76-
77-
assert_equal "ready for database", serialize
82+
assert_equal "serialize(cast(whatever))", attribute.value_for_database
7883
end
7984

8085
test "duping dups the value" do
81-
@type.expect(:deserialize, +"type cast", ["a value"])
8286
attribute = Attribute.from_database(nil, "a value", @type)
8387

84-
value_from_orig = attribute.value
85-
value_from_clone = attribute.dup.value
86-
value_from_orig << " foo"
87-
88-
assert_equal "type cast foo", value_from_orig
89-
assert_equal "type cast", value_from_clone
88+
assert_not_same attribute.value, attribute.dup.value
9089
end
9190

9291
test "duping does not dup the value if it is not dupable" do
93-
@type.expect(:deserialize, false, ["a value"])
94-
attribute = Attribute.from_database(nil, "a value", @type)
92+
@type.define_singleton_method(:deserialize) { |value| value }
93+
attribute = Attribute.from_database(nil, false, @type)
9594

9695
assert_same attribute.value, attribute.dup.value
9796
end
9897

9998
test "duping does not eagerly type cast if we have not yet type cast" do
99+
@type.define_singleton_method(:deserialize) { flunk }
100100
attribute = Attribute.from_database(nil, "a value", @type)
101+
101102
attribute.dup
102103
end
103104

0 commit comments

Comments
 (0)