As I laid out in the start of this series, the goal here is testing beyond what I had been doing up to this point. The background is laid out here.
The next challenge was to test a private function. I didn't find a lot on this issue other than people were wondering how to do it although, admittedly, I didn't look for too long.
Here is the function which, except for the "private" part is the same as the previous method.
How to get around that without compromising the application in production? This might be simplistic and I'm open to debate but I simply extended my production component in another CFC and added a public function like this:
The next challenge was to test a private function. I didn't find a lot on this issue other than people were wondering how to do it although, admittedly, I didn't look for too long.
Here is the function which, except for the "private" part is the same as the previous method.
private function privateSimpleMath(numeric numA, numeric numB){ return numA * numB; }
I tried the same tests but got this (completely expected) error:
How to get around that without compromising the application in production? This might be simplistic and I'm open to debate but I simply extended my production component in another CFC and added a public function like this:
\tests\first\second\third\vanillaCFC\testVanilla.cfc:
component extends="testmods.first.second.third.vanillaCFC" { // Place your content here
public function privateSimpleMath(numeric numA, numeric numB) { return super.privateSimpleMath(numA, numB); } }
The Test Code
component extends="testbox.system.BaseSpec"{ /*********************************** LIFE CYCLE Methods ***********************************/ // executes before all suites+specs in the run() method function beforeAll(){ testobj=createObject("component","pttests.first.second.third.vanillaCFC.testVanilla"); testme=testobj.privateSimpleMath(9,8); } // executes after all suites+specs in the run() method function afterAll(){ } /*********************************** BDD SUITES ***********************************/ function run(){ describe( "My test Suite", function(){ it( "be a number", function(){ expect( testme ).toBeTypeOf('numeric'); }); it( "should be 72", function(){ expect( testme ).toBe(72); }); }); } }
Process and Result
This is the same test as before with the only difference being that I instantiated the CFC which will live in my testing suite and has a public function and extends my actual CFC which will go into production and stays private.
Comments
Post a Comment