New Behaviour classes added

Some of you who have experimented with CFEasyMock may have noticed that during record mode, the actual order of the function calls has no bearing on the way they are actually called and verified. The standard unordered behavior is used on any mock created by MockFactory.createMock().

But there are times when you need either a stricter level of control, or even less control. To accomodate this, CFEasyMock now has Strict and Nice mock behavior.

<cfset mf = createObject( "component", "easymock.MockFactory" ) />
<cfset strict = mf.createStrictMock( "test.component" ) />
<cfset nice = mf.createNiceMock( "test.component" ) />

For strict mocks, the order in which the functions are called during record mode, is matched against the order they are called in replay mode. So not only are you checking to make sure certain functions get called, but also called in a specific order.

Nice mocks are a twist on the standard unordered behavior. You can still add return values and expected function calls, but no errors are thrown by CFEasyMock if those expectations aren't fulfilled.

Let's say I have a Logger.cfc used by my component under test. In this instance, I don't really care if the logger is called during test, as it's primary purpose is just to be used for debugging output. However, I still need to mock it up and pass it into my component, since it's a required parameter.

<cfset mf = createObject( "component", "easymock.MockFactory" ) />
<cfset logMock = mf.createNiceMock( "Logger" ) />
<cfset mf.replay( logMock ) />

Now my test will run, and I won't have the noise of seeing everytime my logMock gets called unexpectedly, or have the overhead of having to declare all the expected behavior.

A note about nice mocks. Nice mocks attempt to return an empty value when there is an unexpected method call (0, "" or false, depending on returntype). If the component under test is expecting an object to be returned, be sure and declare that behavior using MockFactory.expect() or .expectLastCall().

You can also use the new behavior modifier anyTimes(), in addition to atLeastOnce().

<cfset mf = createObject( "component", "easymock.MockFactory" ) />
<cfset mock = mf.createStrictMock( "Logger" ) />

<cfset mf.expect( mock.foo( "Hello World!" ) ).anyTimes()

<cfset mf.replay( mock ) />

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.5.006. | Protected by Akismet | Blog with WordPress