0

I have the following function with image a python object from a module and having the method shape.

def foo(image):
    print image.shape

I want to mock the object image and it's return value

image = Mock()
image.shape.return_value = (0,0)
foo(image)
>>> <Mock name='mock.shape' id='4416382544'>

Calling foo(image), image.shape will return . How can I make it return (0,0)? (I need to get the value to test some condition like if image.shape = (0,0). I'm struggling to have it work.

Thanks for your help

2
  • print isn't return. Commented Jul 23, 2014 at 6:02
  • Also, shape isn't being accessed as a function -- It's a property. You should be able to just do image.shape = (0, 0) where image is the Mock instance. Commented Jul 23, 2014 at 6:04

1 Answer 1

1
image.shape = (0, 0)

Set the attribute directly. return_value has nothing to do with attribute accesses.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Even if i'm using def foo(image): return image.shape ? I'm not sure to see what return_value is for then
@RomanzoCriminale: return_value is for when you're calling something like a function. image.shape.return_value would be what you'd get if you tried image.shape() rather than image.shape.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.