Skip to main content

Setting Up BDD Testing for a Private Method

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.

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

Popular posts from this blog

Creating Stories and Tasks in Jira: Personas and our Software Development Team

Part of the CI/CD Development Series The next step is developing who is on our hypothetical development team. Given that it has a React front end and ColdFusion as the Server Side language, I came up with the following personas, all of which have their own needs and considerations for our development environment. I've listed all the jobs that need doing, not the people involved since, even on a small team or a team of one, these "hats" are all worn by someone, even if it's the same person. Personas for our Project Dev Ops Coordinator - The person responsible for smooth and accurate deployments CF Developer - The person responsible for the API and fulfillment code development and maintenance. React Developer - The person responsible for the front end development Database Coordinator - The person responsible for the schema, data, up time and, presumably the testing databases used by the developers. Lead Developer - The person responsible for coordinat...

CF: Scripting amidst the tags

Recently I had to do a quick "utility" page where I needed to see how many files from a directory listing had been recorded into a database. I've been writing about 98% of my CF code in script syntax but, since it was quick and easy, I did this quickly in tags because it was outputting directly to the browser. In doing so, I made an interesting discovery in that, when you use closures, even in tag based pages, you can write cfscript. Here's the example Get the directory listing:  < cfset alljs = directoryList(expandpath( '/src' ), true , "path" , "*.js" )> Get the database listings and convert it to an array < cfquery name = "alljsQ" datasource = "blah" > select * from sitefiles where filename like '%.js%' </ cfquery > < cfset recordedFiles = valuelist(alljsQ.filename).listToArray()> Use a filter function to weed out the files I'd already recorded < cfset missingFiles = alljs.fi...

As the Dev Ops Manager, I need to start planning our CI/CD release process

Part of the CI/CD Development Series Once we have our Deployment Diagram designed, we need to figure out out to get from here to there. If the end point is the appropriate server environment, the starting point is the developer with his/her hand on the keyboard. These steps take place on a variety of machines, within various process and can changes based on what files are checked in or not. At the moment, we've only created the early basics as seen below. The Beginning of our Deployment Activity Chart Even though there is quite a long way to go there are some elements which we have already been determined. For example We have determined the broad strokes of our technology stack. This is going to be React on the front end and ColdFusion on the server side. We have determined that we are going to be using linting on both the CF and React paths. CFLint for the CF and ESLint for the Javascript We have determined that we are going to be formatters - CFFormat for CF and...