uber / doubles

Test doubles for Python.
MIT License
164 stars 17 forks source link

Match args using specs/constraints rather than just equality #96

Closed nitishr closed 5 years ago

nitishr commented 9 years ago

Most of the time expectations specify literal parameter values that are compared for equality against the actual parameters of invoked methods. For example:

allow(calculator).add(2, 2).and_return(5)

Sometimes, however, you will need to define looser constraints over parameter values to clearly express the intent of the test or to ignore parameters (or parts of parameters) that are not relevant to the behaviour being tested. For example:

allow(calculator).sqrt(less_than(0)).and_raise(ValueError)
allow(log).append(equal_to(Log.ERROR), contains_string("sqrt"))

Loose parameter constraints can be defined by specifying matchers for each parameter. Matchers are created by factory functions, such as less_than, equal_to and contains_string in the example above, to ensure that the expectation is easy to read.

See http://www.jmock.org/matchers.html for motivation and details.

carolinux commented 5 years ago

It would also be interesting to be able to define, in the case of multiple args, that some of the args can have any value.

toddsifleet commented 5 years ago

@carolinux you can do this using equals or mock.ANY.

carolinux commented 5 years ago

thanks! Merry Christmas