derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Suggestion for new assert method #119

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, this is a very nice mocking framework and I've so far found it very useful. 
I have run into one small deficiency with it though. Usually when testing large 
functions i will run into situations where a method is called multiple times 
with different params. Since the default assert_called_with function only tests 
the last call to the mock this is almost always leads me to write code like 
this:

args = self.my_core._getNodeValue.call_args_list
assert(args[6][0][1] == 'media:credit')
assert(args[6][0][2] == "")
assert(args[7][0][1] == 'name')
assert(args[7][0][2] == "Unknown Uploader")

This code is very ugly, and most importantly also very fragile, if anyone at 
some point decides to restructure the sequence of method calls these test will 
break. With the mocking frameworks I'm used to working with the theres usually 
an assert statements that ignores the calls order. 

Since this feature was missing I've changed the Mock class by:
1. Renamed the existing assert_called_with function to assert_called_last_with
2. Created a new assert_called_with that runs trough the call_args_list and 
compares every call with the one provided and raises an AssertionError if a 
match is not found. 

I thought other people might be interested in this behavior so I've attached a 
patch that you may include in the library if you see fit :)

Regards Henrik

Original issue reported on code.google.com by Commande...@gmail.com on 1 Oct 2011 at 10:18

Attachments:

GoogleCodeExporter commented 9 years ago
In 0.8 (beta available - "pip install -U mock==dev") Mock has 
"assert_has_calls" which can be order agnostic. 

See this blog entry:

http://www.voidspace.org.uk/python/weblog/arch_d7_2011_07_30.shtml#e1223

Original comment by fuzzyman on 2 Oct 2011 at 11:53

GoogleCodeExporter commented 9 years ago
Note that changing the semantics of assert_called_with wouldn't be acceptable. 
I think the new assert_any_call is pretty much what you want.

Original comment by fuzzyman on 6 Oct 2011 at 10:22

GoogleCodeExporter commented 9 years ago
hi thanks for the pointer about 0.8 didnt know it was already fixed, I'm 
converting the project to use it now :)

Original comment by Commande...@gmail.com on 7 Oct 2011 at 6:33