Skip to main content

Mocking Unlocked??

I'm a huge fan of both CommandBox and TestBox from OrtusSolutions and have become a believer in  the usefulness of testing. That in and of itself has had it's benefits in making me a more analytical programmer, understanding flow better, forcing me to write shorter methods, etc.

However, for the life of me, I couldn't get to first base on mocking. Conceptually, I knew it was when something was pretending to be something else but I didn't even know enough to understand the documentation (which is more of statement about me than the docs). As a result, I've been using Testbox in a valuable but limited way by using it to run a function without having to go through an interface which may or may not exist yet. The problem is that I wasn't separating it from any other function, table, web call or process with which it interacts. This in an of itself is a valuable process but it's not really unit testing and with that statement comes the following dialogue which I've published somewhere else.

Glossing over the argument about unit testing, we're launching a new automatic build system at work so testing, and more importantly, testing that ran quickly, covered multiple scenarios and was not attached to a database or actually called web services was needed. This precipitated me  actually putting keystrokes to concepts and figuring out how this mocking thing works.

It turns out to be much easier than I thought.

To figure out how this worked I wanted to set up a simple function but one with ever increasing complexity of architecture. By that I mean that ultimately all my function did was to accept two numbers and then return an answer but did so in different ways. The rules were that I had to submit the same two numbers and get an answer. I was using TestBox and it's built in partner, Mockbox. The test could only run the function being tested and all information needed to run that function had to originate with the test, no outside dependencies.

The scenarios that I put together were these:

1. A public function which accepted two numbers, multiplied them together and returned the answer
2. A private function which accepted two numbers, multiplied them together and returned the answer
3.  A public function which which accepted two numbers, queried a database for the answer and returned it.
4. A public function which accepted two numbers, obtained a query from another function in the component and then returned the answer. This actually used MockBox for the first time..

These went surprisingly well and I'll be outlining other techniques as I explore this more thoroughly.



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...