This is part of a [short but hopefully growing] series on mocking.
There are a several presentations and articles about the need for testing and also how to set up up a framework like TestBox. I'll summarize:
2. Open CommandBox
3. Type "mkdir testproject true" - this will create a folder called testproject and then move you into it.
4. Type "install testbox" - This will install TestBox.
5. Type "server start" - This will open up a browser serving CFML from Lucee with the root of the site being the testproject folder.
6. Open up your development software and point it at this folder.
I created two folders
1. "testMods" to hold the CFCs and methods I wanted to test
2.. "tests" to hold the unit tests I was going to make.
The CFC which contained the methods to test was located at testMods.first.second.third.VanillaCFC.cfc
The folder which housed the tests was \tests\first\second\third\vanillaCFC\. I have no idea about best practices but it seems a good idea to me to house the tests in the same folder structure as the actual methods for simple organizational purposes. Each CFC to test had a folder in the \tests\path and then the tests for that CFC went int that folder.
The last piece of setup was to copy the index.cfm page from the \testbox\test-browser folder into my \tests folder and change the rootmapping to my folder. That will let us browse to the test we want to run
This method is easy to test. It's self contained, publicly available, has a definitive answer which is a straightforward, simple variable.
Using CommandBox to create a skeleton of the tet for this method by navigating to \tests\first\second\third\vanillaCFC and typing
testbox create bdd simpleMath
We get a simple document which I then filled in with the beforeAll Content and then my tests:
There are a several presentations and articles about the need for testing and also how to set up up a framework like TestBox. I'll summarize:
Setup
1. Download and install CommandBox2. Open CommandBox
3. Type "mkdir testproject true" - this will create a folder called testproject and then move you into it.
4. Type "install testbox" - This will install TestBox.
5. Type "server start" - This will open up a browser serving CFML from Lucee with the root of the site being the testproject folder.
6. Open up your development software and point it at this folder.
I created two folders
1. "testMods" to hold the CFCs and methods I wanted to test
2.. "tests" to hold the unit tests I was going to make.
The CFC which contained the methods to test was located at testMods.first.second.third.VanillaCFC.cfc
The folder which housed the tests was \tests\first\second\third\vanillaCFC\. I have no idea about best practices but it seems a good idea to me to house the tests in the same folder structure as the actual methods for simple organizational purposes. Each CFC to test had a folder in the \tests\path and then the tests for that CFC went int that folder.
The last piece of setup was to copy the index.cfm page from the \testbox\test-browser folder into my \tests folder and change the rootmapping to my folder. That will let us browse to the test we want to run
<!--- SETUP THE ROOTS OF THE BROWSER RIGHT HERE --->
<cfset rootMapping = "/tests">
Testing the Public Function
The method here is very simple. It accepts two number, multiplies them and returns the answer.
component accessors="true"{
public function simpleMath(numeric numA, numeric numB){ return numA * numB; }
}
This method is easy to test. It's self contained, publicly available, has a definitive answer which is a straightforward, simple variable.
Using CommandBox to create a skeleton of the tet for this method by navigating to \tests\first\second\third\vanillaCFC and typing
testbox create bdd simpleMath
We get a simple document which I then filled in with the beforeAll Content and then my tests:
/*** My BDD Test*/
component extends="testbox.system.BaseSpec"{ /*********************************** LIFE CYCLE Methods ***********************************/ // executes before all suites+specs in the run() method
function beforeAll(){ testobj=createObject("component","testmods.first.second.third.vanillaCFC"); testme=testobj.simpleMath(8,9); } // executes after all suites+specs in the run() method function afterAll(){ } /*********************************** BDD SUITES ***********************************/ function run(){ describe( "The simpleMath result should...", function(){ it( "be a number", function(){ expect( testme ).toBeTypeOf('numeric'); }); it( "be 72 given the inputs 8 and 9", function(){ expect( testme ).toBe(72); }); }); } }
Process and Result
This is about as simple as it gets for testing. In the beforeAll() function we use createObject to make an instance of the vanillaCFC called testObj and then we make a variable called testme which is teh result of the simpleMath(8,9). We then submit testme to some expectations, namely that it should be a number and that it should equal 72.
Good to go. On to the next challenge: Private methods!
Comments
Post a Comment