Skip to main content

Posts

Showing posts from January, 2018

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

Mocking Methods - FINALLY!

This is part of a series of posts on Testing and Mocking . The next (and final) challenge for this evening was to adapt the query method to retrieve the query from another method in the same component rather than the component's property. This is almost identical to the previous except without using a mocking tool we can't easily change a method call or what a function returns at run time. This is our first case of using a tool like MockBox. public function mathFromFunctionQuery( numeric numA, numeric numB){ local .qc=new query(dbtype= "query" , sql= "select answer from sourceQuery where numA=:numA and numB=:numB" ); local .qc.addParam(name= "numA" ,value= arguments .numA); local .qc.addParam(name= "numB" ,value= arguments .numB); local .qc.setAttributes(sourceQuery= this .getQ() ); <---different part. from method, not property. local .res= local .qc.execute().getresult(); return local .res.answer[

Why Test and Mock - dramatized

The following was a sleep induced dialogue about using mocks in testing and that running tests against each function in isolation,even if it uses actual live code in its dependencies. Q: Why isn't that unit testing? A: Because Unit Testing when you test one function and only that function. Q: Isn't that just semantics and an argument used by testing snobs? Who cares as long as the test is seeing if the code works? A: That might work for simple functions but as soon as your code touches another function you are then testing two functions. So if the code breaks, which function is broken? Q: You run the tests for the other function. It's like bricks, you build and test the code on the "bottom" of the dependency chain and then work your way up. A: Do you have a map of all your code and what depends on what at all times? How do you know in what order to run your tests? Q: Well, once the bottom layer is done, it shouldn't change. A: Ah, so you never refa

Mocking a Query

This is the third (or fourth) in a series about recent discoveries in testing and mocking . This challenge was to accept two numbers and use them to query a database to get the result. Is this a real world example? Maybe not for math but obviously we use DBs all the time so that part is real. The function starts off simple enough. public function mathFromQuery( numeric numA, numeric numB){ } We're still accepting the two numbers. then we run into the crux of the issue which is how are we to we provide the results of a query without actually calling the DB which may or may not exist in our testing environment. This will not work: public function mathFromQuery( numeric numA, numeric numB){ local.qs=new query(datasource=blah,sql="select * from blah where blah blah blah"); } Since the query originates from the function we have no way of controlling its contents except to provide a db. The only way that anything can exists from the outside of a functio

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

Setting Up Testing for a Simple Math Function

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: Setup 1. Download and install CommandBox 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\seco