How do I use Python Mox to check if a method is called with the equivalent of a string?

397 Views Asked by At

I want to check if a function is being called with a string - however it seems that it makes a difference if that string is created using '%s' to insert a substring. The test is failing with this error:

UnexpectedMethodCallError: Unexpected method call.  unexpected:-  expected:+
- foo('Hello %s', 'world') -> None
?            ------
+ foo('Hello world') -> None

How do I check if the method is being called on 'Hello world', regardless of how the string was created?

2

There are 2 best solutions below

0
On BEST ANSWER

There is no type or class like "formatted string" in the python. Just string. I think your have error in your code. I mean, it is your fault. I bet you are trying to call it like this:

foo('Hello %s', 'world')

But you should, instead, do

foo('Hello %s' % 'world')

Which will give you expected result.

0
On

You should be doing

'Hello %s' % 'world'

There are also a series of comparators that may help you. If you do not care what the value of the string is, then call: mox.IsA(str). You can also do mox.StrContains or mox.Regex to validate further.