uber / doubles

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

allow hamcrest matchers as arg specs #97

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 are 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.

Fixes #96

toddsifleet commented 9 years ago

@nitishr checkout https://github.com/toddsifleet/equals, which allows for:

expect(test_object).method.with_args(any_string.containing('bob'))