uber/doubles

Match args using specs/constraints rather than just equality

Closed this issue · 3 comments

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.

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.

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

thanks! Merry Christmas