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

More on Site Architecture / CI/CD / and Repos

We're starting to move from the high level overview and more into the details of our project.  Architecture We established that we using React as our front end technology and ColdFusion as our server side. At a more granular level, we'll be using React with Redux for our front end app and we want our server side to be not just "ajax" enabled but a true REST API. To this end, we're going to incorporate Coldbox from Ortus Solutions. Repos These two code bases have different needs, and possibly locations, when they are deployed. As a result, we're going to have two repositories: one for the React and another for the API. This will allow us to keep our code separated and cleaner all the way through to deployment.  For each of these repos, we'll follow the procedure we laid out previously with the MASTER branch being used to deploy to production and a DEVELOPMENT branch being used for active work which is then fed into the Master branch via pull requests.  Test...