问题
I want to test a method that calls the pylons.url object. However calling this in the tests leads to an error:
TypeError: No object (name: url) has been registered for this thread
So I'd like to replace the pylons.url object with a Mock from the mock library.
@patch('pylons.url')
def my_test(self, url_mock):
...
However, this doesn't seem to replace the url object.
Is there a way to mock out this object?
回答1:
In order for patch to work you need to give it the full path to the variable in the MODULE that you are actually patching. So rather than 'pylons.url' you would be patching 'my_project.my_module.url' and inside my_module.py you would be doing
from pylons import url
Does that make sense?
来源:https://stackoverflow.com/questions/4275389/mocking-away-the-url-object-in-pylons