Mock an object in a method that is not a parameter in python
(i'm a python newbie)
I was looking at: Mocking out objects and methods
And I was wondering if i can replace an object in a python method which is not passed as a parameter, lets say my method is like this:
def mypymethod(param1)
myresult = os.path.something
return myresult
and i wish to test mypymethod however i wish os.path at th开发者_高级运维is case to return "yourmockresult" when i call it from my test method
anyway i can do this? thanks!!
You should use Michael Foord's Mock module for the same.
From the documentation:
>>> from mock import Mock
>>> real = ProductionClass()
>>> real.method = Mock(return_value=3)
>>> real.method(3, 4, 5, key='value')
3
>>> real.method.assert_called_with(3, 4, 5, key='value')
精彩评论