@@ -7,9 +7,13 @@ it's easy to define custom argument matchers to meet your specific needs as well
77There's nothing magical about matchers. Any object passed into a ` when() ` or
88` verify() ` invocation that has a ` __matches ` function on it and returns truthy
99when it matches and falsey when it doesn't is considered a matcher by
10- testdouble.js. Anything without a ` __matches ` function will only match an
10+ testdouble.js. That said, we provide a ` td.matchers.create() ` helper for creating
11+ your own custom matchers in a way that'll help ensure your users will get better
12+ messages from ` td.explain ` calls and ` td.verify ` failures.
13+
14+ (For the record, arguments without a ` __matches ` property will only match an
1115actual invocation if it passes lodash's deep
12- [ _ .isEqual] ( https://lodash.com/docs#isEqual ) test.
16+ [ _ .isEqual] ( https://lodash.com/docs#isEqual ) test.)
1317
1418The examples in this document assume you've aliased ` testdouble ` to ` td ` .
1519
@@ -20,13 +24,13 @@ the expected type of an argument matches the type of the argument actually passe
2024to the test double function from the subject under test.
2125
2226``` javascript
23- isA = function ( expected ) {
24- return {
25- __matches : function (actual ) {
26- return actual instanceof expected;
27- }
27+ isA = td . matchers . create ( {
28+ name : ' isA ' ,
29+ matches : function (matcherArgs , actual ) {
30+ var expected = matcherArgs[ 0 ]
31+ return actual instanceof expected
2832 }
29- }
33+ })
3034```
3135
3236Once defined, the above function can be used in a test like this:
@@ -39,3 +43,24 @@ td.when(datePicker(isA(Date))).thenReturn('good')
3943datePicker (new Date ()) // 'good'
4044datePicker (5 ) // undefined
4145```
46+
47+ #### td.matchers.create API
48+
49+ The ` create ` function takes a configuration object with the following properties
50+
51+ * ** matches(matcherArgs, actual)** - _ required_ - a function that returns truthy
52+ when an ` actual ` argument satisfies the matcher's rules, given what the user
53+ passed to the matcher as ` matcherArgs ` when setting it up. For instance, if
54+ ` td.when(func(isFooBar('foo','bar')).thenReturn('baz') ` is configured, then
55+ ` func('biz') ` is invoked, then ` isFooBar ` 's ` matches ` function will be invoked
56+ with ` matcherArgs ` of ` ['foo','bar'] ` and ` actual ` of ` 'biz' `
57+ * ** name** - _ optional_ - a string name for better messages. A function can
58+ also be provided, which will be passed the user's ` matcherArgs ` and should return
59+ a string name
60+ * ** onCreate(matcherInstance, matcherArgs)** - _ optional_ - a function invoked
61+ whenever an instance of a matcher is created to give the matcher author an
62+ opportunity to mutate the matcher instance or have some other side effect. The
63+ ` td.callback ` functionality of the library depends on this option
64+
65+ For some examples of ` td.matchers.create() ` in action, check out the
66+ [ built-in matchers] ( src/matchers/index.coffee ) provided by testdouble.js.
0 commit comments