I am try to mock out an urllib2.urlopen that will raise a HTTPError 404. This is what I have so far:
import urllib2
import mox
# check that it works
req = urllib2.Request('http://www.google.com/')
print urllib2.urlopen(req)
# OK, let's mock it up
m = mox.Mox()
m.StubOutWithMock(urllib2, 'urlopen')
# We can be verbose if we want to :)
import socket
f = m.CreateMock(socket._fileobject)
f.read().AndReturn("Testing")
urllib2.urlopen(mox.IsA(urllib2.Request)).AndRaise(urllib2.HTTPError('http://google.com',404, "test", {}, f))
# Let's check if it works
m.ReplayAll()
try:
req = urllib2.Request('http://www.google.com/')
print urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.read()
# yay! now unset everything
m.UnsetStubs()
m.VerifyAll()
# and check that it still works
#### This is the output:
<addinfourl at 169648044 whose fp = <socket._fileobject object at 0xb7cd3f44>>
read() -> None
Traceback (most recent call last):
File "mox-test.py", line 31, in <module>
m.VerifyAll()
File "build/bdist.linux-i686/egg/mox.py", line 197, in VerifyAll
File "build/bdist.linux-i686/egg/mox.py", line 344, in _Verify
mox.ExpectedMethodCallsError: Verify: Expected methods never called:
0. read() -> 'Testing'
1. read() -> None
I am very new to pymox so any help would be awesome. Thanks.