forked from trobert2/unittest_repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_first_mox.py
62 lines (45 loc) · 1.56 KB
/
test_first_mox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import unittest
import mox
import urllib2
import first
class FirstTest(unittest.TestCase):
def setUp(self):
#we are working with the mox module
self.mox = mox.Mox()
#the method we are testing will be called from this object
self.obj = first.First()
#stub the methods not in your layer for testing
self.mox.StubOutClassWithMocks(urllib2, 'Request')
self.mox.StubOutWithMock(urllib2, 'urlopen')
def tearDown(self):
self.mox.UnsetStubs()
def test_get_data(self):
request = urllib2.Request(mox.IsA(str))
m = urllib2.urlopen(request)
handle = self.mox.CreateMockAnything()
m.AndReturn(handle)
m = handle.read()
m.AndReturn('value ni!')
#put everything in replay mode
self.mox.ReplayAll()
response = self.obj.get_data('string')
#verify, check called/expected methods
self.mox.VerifyAll()
self.assertEqual(response, 'value ni!')
def test_get_data_exception(self):
request = urllib2.Request(mox.IsA(str))
m = urllib2.urlopen(request)
m.AndRaise(urllib2.HTTPError('string',
404,
'test error 404',
{},
None))
self.mox.ReplayAll()
self.assertRaises(first.DataNotFound, self.obj.get_data,
'string')
self.mox.VerifyAll()
#def main():
# unittest.main()
#
#if __name__ == "__main__":
# main()