In my previous post, I discussed the importance of refactoring and a general workflow for using Approval Tests to be sure your output is not changing.
In this blog series, I hope to give some examples of how to use the TestBox-Snapshots package for refactoring legacy CFML. The first post will walk you through installing the package and setting up a test suite to capture an initial snapshot.
I created a legacy CFML style To-Do application to use as an example of refactoring CFML using Approval Tests. Feel free to clone the repo and play along at home if you like.
Then you run the application using CommandBox.
First, we need to install TestBox and the TestBox-Snaphots package so we can take an initial snapshot.
Now we copy the TestBox test harness and create the specs folder for my tests.
I will create a folder in my specs directory specifically for my approval tests.
Next, we will generate a test suite that will contain our approval tests. You can run the tests by calling the
/tests/runner.cfm from the browser.
We then add the snapshot matchers to TestBox so we can use it for our Approval Tests. To do this we use the
addMatchers method inside the
Now we can start writing some approval tests to get our initial snapshots. First I will create a test suite named
RefactorApprovalTest inside the
run() method. I use the
beforeEach method to execute a query that inserts some dummy data for testing. This will run prior to each test. It also resets the identity column on the todo table so the keys will match each time we run the test.
For our first approval test, I use the
it() method inside my test suite. I include the index page with no parameters and capture the output using
cfsavecontent. Then use the snapshot matcher to check that the snapshot matches.
With legacy code you may not always know what the code is supposed to do so you could also use a generic description such as
it("should match previous output with not parameters",....
When you run the test it will fail because it does not have a snapshot to compare to. Run the test and include
updateSnapshots=1 in the URL query string. Be sure to remove it when you start to refactor or it will create a new snapshot each time you run the tests.
I added more tests to cover the following three scenarios.
- Adding a new to-do
- Completing to-do items
- View completed to-do items
We now have a suite of approval tests that will fail if the output changes. This will allow us to start refactoring the code with confidence. You can view the full test suite here. The next post we will start to refactor the code and use the test suite to verify that things have not changed.