Matchers
I just ran into something that brought my unit testing to a halt. Strings. Not just any strings, but UUIDs in this case. The code under test calls a mutator method with a generated UUID, like so:
How do you set up that behavior? Well, in the current version of CFEasyMock, you can't do that. You can monkey with the MockFactory.equalsArgs() method and add an exception (not the error kind) in the logic to check for a specific string and then return a match, but that's a bit of a hack.
Enter matchers. v1.6 will add the following pattern matchers to the MockFactory object:
matches( regexpr ) Matches if the actual value matches with the regular expression given.
startsWith( String )
contains( String )
endsWith( String ) Matches if the actual value startsWith/contains/endsWith the given value.
anyBoolean()
anyString()
anyNumber()
anyComponent() Matches any value.
The expected usage for the uuid problem could be solved in a number of way, but the matches() would provide the best comparison. Using anyString() would be too loose.
<cfset user = mf.createMock( "my.User" ) />
<cfset mf.expect( user.setId( mf.matches( "^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{16}$" ) ) ) />
<cfset mf.replay( user ) />
...
<cfset mf.verify( user ) />
Hmmmm... looks like I might have to add an anyGUID() function as well.

There are no comments for this entry.
[Add Comment]