Mock urlopen AndRaise HTTPError

344 views
Skip to first unread message

Vbabiy

unread,
Mar 20, 2009, 4:23:16 PM3/20/09
to mox-discuss
I can't seem to get this to work, Here 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


## Here is the output
python mox-test.py
<addinfourl at 145792940 whose fp = <socket._fileobject object at
0xb7da5f44>>
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

steve middlekauff

unread,
Mar 20, 2009, 5:05:10 PM3/20/09
to mox-d...@googlegroups.com
Your code looks sound to me, so maybe it's something that is happening
in the urllib2 code? I haven't ever used that library before, so I
can't be sure. Maybe someone with more experience will spot your
issue.
--
Steve Middlekauff
smid...@gmail.com

Vbabiy

unread,
Mar 20, 2009, 9:52:12 PM3/20/09
to mox-discuss
Here is some more details If I create my own mock object and pass that
in which follows the correct protocol:
class TestObject(object):
def read(*args, **kargs):
print "I am being Read"
return "readTest"

def readline(*args, **kargs):
print "Printline is being run"
return "TestRead"

urllib2.urlopen(mox.IsA(urllib2.Request)).AndRaise(urllib2.HTTPError
('http://google.com',404, "test", {}, TestObject()))

The output is correct:
python mox-test.py
<addinfourl at 3081497388L whose fp = <socket._fileobject object at
0xb7d39b54>>
I am being Read
readTest


But When I pass in a mock objected created by mox:
f = m.CreateMockAnything()
f.read(mox.IgnoreArg()).AndReturn("Page not found")

urllib2.urlopen(mox.IsA(urllib2.Request)).AndRaise(urllib2.HTTPError
('http://google.com',404, "test", {}, f))

This is my output:
python mox-test.py
<addinfourl at 3082164748L whose fp = <socket._fileobject object at
0xb7e68a3c>>
read() -> None # NO IDEA WHERE THIS IS COMING FROM
Traceback (most recent call last):
File "mox-test.py", line 40, in <module>
m.VerifyAll()
File "build/bdist.linux-i686/egg/mox.py", line 198, in VerifyAll
File "build/bdist.linux-i686/egg/mox.py", line 347, in _Verify
mox.ExpectedMethodCallsError: Verify: Expected methods never called:
0. read(<IgnoreArg>) -> 'Page not found'
1. read() -> None # ALSO NO IDEA WHERE THIS IS COMING FROM
> smidd...@gmail.com
Reply all
Reply to author
Forward
0 new messages