[MockBox] $count on SUT method

Hi,

Question on using spies with mockbox. Consider this:

/* Thing.cfc */
component{

function init(){
variables.counter = 0;
}

function doSomething(foo){
if (foo){
addit();
}
}

private function addit(){
variables.counter++;
}

}

What I want to do is test that the ‘addit’ method is called by the ‘doSomething’ method when arguments.foo is true. This is what I have in my test:

CUT = new model.Thing();
prepareMock(CUT);

CUT.doSomething(true);
assertEquals(1, CUT.$count(“addit”));

I can use the $getProperty method to do it, just wondered if you could use the $count method that’s injected.

…update…

you can do it by mocking the method, so I’ll do that, so this gives the desired result:

CUT = new model.Thing();
prepareMock(CUT);
CUT.$(“addit”);
CUT.doSomething(true);
assertEquals(1, CUT.$count(“addit”));

Your code looks good.

Just remember that you will probably want to call CUT.reset(“addit”); or CUT.reset() between tests otherwise the counter will just keep counting and your asserts will be order dependent. This is generally only applicable when running multiple asserts/tests within a particular unit test. If you always do CUT = new model.Thing(), that will also do the trick.