3
3
exacty the same as unittest.mock.Mock).
4
4
"""
5
5
6
+ import upytest
6
7
from umock import Mock
7
8
8
9
9
10
def test_init_mock ():
10
11
"""
11
- A Mock object should be created with no attributes.
12
+ A plain Mock object can be created with no arguments. Accessing arbitrary
13
+ attributes on such an object (without a spec) should return another Mock
14
+ object.
12
15
"""
13
16
mock = Mock ()
14
- assert mock .__dict__ == {}, "Not an empty dict."
17
+ assert mock .call_count == 0 , "Non zero call count with new Mock object."
18
+ assert isinstance (
19
+ mock .foo , Mock
20
+ ), "Attribute access did not return a Mock."
15
21
16
- def test_init_mock_with_spec ():
22
+
23
+ def test_init_mock_with_spec_from_list ():
24
+ """
25
+ A Mock object should be created with the specified list of attributes.
26
+ Accessing arbitrary attributes not in the list should raise an
27
+ AttributeError.
28
+
29
+ If an arbitrary attribute is subqeuently added to the mock object, it
30
+ should be accessible as per normal Python behaviour.
31
+ """
32
+ mock = Mock (spec = ["foo" , "bar" ])
33
+ assert hasattr (mock , "foo" ), "Mock object missing 'foo' attribute."
34
+ assert hasattr (mock , "bar" ), "Mock object missing 'bar' attribute."
35
+ assert not hasattr (
36
+ mock , "baz"
37
+ ), "Mock object has unexpected 'baz' attribute."
38
+ mock .baz = "test"
39
+ assert mock .baz == "test" , "Mock object attribute 'baz' not set correctly."
40
+
41
+
42
+ def test_init_mock_with_spec_from_object ():
43
+ """
44
+ A Mock object should be created with the specified attributes derived from
45
+ the referenced instance. The Mock's __class__ should be set to that of the
46
+ spec object's. Accessing arbitrary attributes not on the class should raise
47
+ an AttributeError.
48
+
49
+ If an arbitrary attribute is subqeuently added to the mock object, it
50
+ should be accessible as per normal Python behaviour.
51
+ """
52
+
53
+ class TestClass :
54
+ x = 1
55
+ y = 2
56
+
57
+ obj = TestClass ()
58
+ mock = Mock (spec = obj )
59
+ assert hasattr (mock , "x" ), "Mock object missing 'x' attribute."
60
+ assert hasattr (mock , "y" ), "Mock object missing 'y' attribute."
61
+ assert not hasattr (mock , "z" ), "Mock object has unexpected 'z' attribute."
62
+ assert mock .__class__ == TestClass , "Mock object has unexpected class."
63
+ mock .z = "test"
64
+ assert mock .z == "test" , "Mock object attribute 'z' not set correctly."
65
+
66
+
67
+ def test_init_mock_with_spec_from_class ():
68
+ """
69
+ A Mock object should be created with the specified attributes derived from
70
+ the referenced class. Since this is a class spec, the Mock's __class__
71
+ remains as Mock. Accessing arbitrary attributes not on the class should
72
+ raise an AttributeError.
73
+
74
+ If an arbitrary attribute is subqeuently added to the mock object, it
75
+ should be accessible as per normal Python behaviour.
76
+ """
77
+
78
+ class TestClass :
79
+ x = 1
80
+ y = 2
81
+
82
+ mock = Mock (spec = TestClass )
83
+ assert hasattr (mock , "x" ), "Mock object missing 'x' attribute."
84
+ assert hasattr (mock , "y" ), "Mock object missing 'y' attribute."
85
+ assert not hasattr (mock , "z" ), "Mock object has unexpected 'z' attribute."
86
+ assert mock .__class__ == Mock , "Mock object has unexpected class."
87
+ mock .z = "test"
88
+ assert mock .z == "test" , "Mock object attribute 'z' not set correctly."
89
+
90
+
91
+ def test_init_mock_with_callable_side_effect ():
92
+ """
93
+ A Mock object should be created with the specified callable side effect
94
+ that computes the result of a call on the mock object.
95
+ """
96
+
97
+ def side_effect (a , b ):
98
+ return a + b
99
+
100
+ mock = Mock (side_effect = side_effect )
101
+ assert (
102
+ mock (1 , 2 ) == 3
103
+ ), "Mock object side effect did not compute correctly."
104
+
105
+
106
+ def test_init_mock_with_exception_class_side_effect ():
107
+ """
108
+ A Mock object should be created with the specified exception class side
109
+ effect that raises the exception when the mock object is called.
110
+ """
111
+
112
+ class TestException (Exception ):
113
+ pass
114
+
115
+ mock = Mock (side_effect = TestException )
116
+ with upytest .raises (TestException ):
117
+ mock ()
118
+
119
+
120
+ def test_init_mock_with_exception_instance_side_effect ():
121
+ """
122
+ A Mock object should be created with the specified exception instance side
123
+ effect that is raised when the mock object is called.
124
+ """
125
+
126
+ ex = ValueError ("test" )
127
+ mock = Mock (side_effect = ex )
128
+ with upytest .raises (ValueError ) as expected :
129
+ mock ()
130
+ assert (
131
+ str (expected .exception .value ) == "test"
132
+ ), "Exception message not as expected."
133
+
134
+
135
+ def test_init_mock_with_iterable_side_effect ():
136
+ """
137
+ A Mock object should be created with the specified iterable side effect
138
+ that returns the next item in the iterable each time the mock object is
139
+ called.
140
+ """
141
+
142
+ mock = Mock (side_effect = [1 , 2 , 3 ])
143
+ assert mock () == 1 , "First call did not return 1."
144
+ assert mock () == 2 , "Second call did not return 2."
145
+ assert mock () == 3 , "Third call did not return 3."
146
+ with upytest .raises (StopIteration ):
147
+ mock ()
148
+
149
+ def test_init_mock_with_invalid_side_effect ():
17
150
"""
18
- A Mock object should be created with the specified attributes .
151
+ If an invalid side effect is specified, a TypeError should be raised .
19
152
"""
20
- pass
153
+ mock = Mock (side_effect = 1 )
154
+ with upytest .raises (TypeError ):
155
+ mock ()
21
156
22
- def test_init_mock_with_spec_and_values ():
157
+ def test_init_mock_with_return_value ():
23
158
"""
24
- A Mock object should be created with the specified attributes and values.
159
+ A Mock object should be created with the specified return value that is
160
+ returned each time the mock object is called.
25
161
"""
26
- pass
162
+ mock = Mock (return_value = 42 )
163
+ assert mock () == 42 , "Return value not as expected."
0 commit comments