1
+ import requests_mock
1
2
from unittest import mock
2
3
3
4
import django_dynamic_fixture as fixture
@@ -11,44 +12,21 @@ class GoldSignalTests(TestCase):
11
12
def setUp (self ):
12
13
self .user = fixture .get (User )
13
14
14
- # Mocking
15
- self .patches = {}
16
- self .mocks = {}
17
- self .patches ["requestor" ] = mock .patch ("stripe.api_requestor.APIRequestor" )
18
-
19
- for patch in self .patches :
20
- self .mocks [patch ] = self .patches [patch ].start ()
21
-
22
- self .mocks ["request" ] = self .mocks ["requestor" ].return_value
23
-
24
- def tearDown (self ):
25
- # TODO: refactor to use request_mocks for these. The current mock
26
- # pattern breaks tests ran after these if tearDown is not called with
27
- # the `.stop` for all the patches. It took me a lot of time to realized
28
- # about this problem.
29
- for _ , patch in self .patches .items ():
30
- patch .stop ()
31
-
32
- def mock_request (self , resp = None ):
33
- if resp is None :
34
- resp = ({}, "" )
35
- self .mocks ["request" ].request = mock .Mock (side_effect = resp )
36
-
37
- def test_delete_subscription (self ):
15
+ @requests_mock .Mocker (kw = "mock_request" )
16
+ def test_delete_subscription (self , mock_request ):
38
17
subscription = fixture .get (GoldUser , user = self .user , stripe_id = "cus_123" )
39
18
self .assertIsNotNone (subscription )
40
- self .mock_request (
41
- [
42
- ({"id" : "cus_123" , "object" : "customer" }, "" ),
43
- ({"deleted" : True , "customer" : "cus_123" }, "" ),
44
- ]
19
+ mock_request .get (
20
+ "https://api.stripe.com/v1/customers/cus_123" ,
21
+ json = {"id" : "cus_123" , "object" : "customer" },
22
+ )
23
+ mock_request .delete (
24
+ "https://api.stripe.com/v1/customers/cus_123" ,
25
+ json = {"deleted" : True , "customer" : "cus_123" },
45
26
)
46
27
47
28
subscription .delete ()
48
-
49
- self .mocks ["request" ].request .assert_has_calls (
50
- [
51
- mock .call ("get" , "/v1/customers/cus_123" , {}, mock .ANY ),
52
- mock .call ("delete" , "/v1/customers/cus_123" , {}, mock .ANY ),
53
- ]
54
- )
29
+ assert mock_request .request_history [0 ]._request .method == "GET"
30
+ assert mock_request .request_history [0 ]._request .url == "https://api.stripe.com/v1/customers/cus_123"
31
+ assert mock_request .request_history [1 ]._request .method == "DELETE"
32
+ assert mock_request .request_history [1 ]._request .url == "https://api.stripe.com/v1/customers/cus_123"
0 commit comments