Hey all,pretty new to CFML and just started workin...
# testing
b
Hey all,pretty new to CFML and just started working on an application using Lucee (no frameworks or coldbox)/Command box/MySQL containerized with docker. I'd like to attempt to add tests with TestBox, but am struggling getting a clear idea of not only how to add the testing setup, but how to write some of the tests. The docker containers are: • Command box v4 to start Lucee server • Httpd v2 to serve the web app • MySQL v8 How can I setup testing? The app currently has zero tests or any infra for it. Do I just add a run command to install TestBox in the command box Docker file? Any thing I need to watch out for setting up config for container environments (I assume I need to expose a port for the test UI, etc..)? Just trying to get my bearings on this and would appreciate pointers if someone else was in a similar scenario. Also is there up to date resources or video tutorials for writing tests for business/query logic in apps that don't use coldbox?
a
Welcome 🙂 First step is to install Testbox. You can do that by running this your project root folder
box install testbox
There's a bunch of free videos on cfcasts that might be of interest https://cfcasts.com/browse?q=testbox
You can run the tests using port 80, so a webserver virtual directory might be a good idea so you don't deploy it to production.
b
Thanks, will definitely check out the linked videos. Part of what I'm trying to get a clear picture on is what I need to do to get set up in a docker containerized environment (for local development using docker compose which is already in place to start the containers listed above). At some point would like to get it in a pipeline, but for now just need to get something integrated and working for tests. So I assume I can add a RUN box install TestBox in the commandbox dockerfile (port 80 is expised already i believe). And then, create a volume/bind mount if needed to a tests directory. Something like that? Just still trying to put it all together, but will keep reading (and asking questions here)
a
Are you mounting a directory using docker or is all your code in the docker container?
If you are mounting a drive on your local machine which is where all your app code is, then you can just do the
box install testbox
in your mounted app root.
b
There is a bind mount:
.:/app
so all code should be mounted to the container (I can make changes, refresh the page in browser and see the updated app running from the container)
a
yeah - that's the one. So you can run the commandbox commands on your local machine when you are in that working directory.
You don't need it to be part of the build process (although you can do that if preferred)
b
Got it. What if another developer pulls the repo? Or if I docker-compose down and then start up the containers the next day? Do I/we need to run box install TestBox everytime or is that persisted somehow (since I'm just running it from a mounted local folder/environment and will need to shutdown the container). Let me know if I'm misunderstanding though
a
You have a choice (always the choices!) you can either just commit the installed testbox files to your repo, so it'll be part of your repo. Or you can just ask other developers to run
box install
after pulling which will mean they get the latest and greatest packages (commandbox has a package manager if you are familiar with something like npm)
when you do
box install textbox
it'll create a
box.json
file which tracks dependancies and versions etc, which is how the
box install
knows what to go and grab.
> Or if I docker-compose down and then start up the containers the next day? Testbox will install into your mounted directory so won't get blown away with the container.
b
Ah, ok yeah I'm coming from a react/node background using npm etc. I'm trying to figure out how similar works in CFML app ecosystem, so that's very helpful. So, I would want to commit testing setup to the repo, in that case sounds like I: • Run box install TestBox from my local drive (in my project folder) • Commit the box.json • Add to readme for new devs to run box install when pulling for first time
👍 1
a
you'll want to gitignore the
testbox
directory, we don't have a
node_modules
folder in cfml and like you do in node.
👍 1
b
After giving that a try I'll dig into those vids you linked and see if I can find examples of testing. The thing I'm seeing is most of the examples use coldbox which unfortunately we're not using.
Oh got it on gitiginore, was going to ask about that. Thx
a
You can use testbox without coldbox - just from the same team of smart people
A test looks something like this:
Copy code
component extends="testbox.system.BaseSpec" {

	/*********************************** LIFE CYCLE Methods ***********************************/

	// executes before all suites+specs in the run() method
	function beforeAll(){
	}

	// executes after all suites+specs in the run() method
	function afterAll(){
	}

	/*********************************** BDD SUITES ***********************************/

	function run(){
		feature( "Given-When-Then test language support", function(){
			scenario( "I want to be able to write tests using Given-When-Then language", function(){
				given( "I am using TestBox", function(){
					when( "I run this test suite", function(){
						then( "it should be supported", function(){
							expect( true ).toBe( true );
						} );
					} );
				} );
			} );
		} );
	}

}
(if you like given-when-then language when writing tests)
oh and as you're used to JS, then you can use fat arrow syntax for functions in CFML as well.
b
Nice. One thing I'm having trouble wrapping my head around is how do you hook up tests to your CFML components? Like in react you use jest and node setup to import a component, render it and then test it. How do you do that in CFML apps? (I e. There is no import statement, but I see include and extends which look similar..).
a
We do have
import
so you can do that, it's more like Java import than commonjs import though
OK so you can do something like this:
b
Ah, do you know of a good resource or example of a test that does that I can look at? (Oh I see you're writing one....ty!)
a
Copy code
component extends="testbox.system.BaseSpec" {

  function run( testResults, testBox ) {
    describe( "config bean", function() {
      it( "should do stuff", function() {
        var sut = new model.Config();
        expect( sut.stuff() ).toBe( true );
	  }
    }
  }
}
I think that'll work - I just typed it out - but hopefully shows the idea at least
f
Here’s an example / template for doing it in CI with github actions: https://github.com/foundeo/cfml-ci-examples/blob/master/.github/workflows/release.yml
🙏 1
also handy reference for testbox: https://cfdocs.org/testbox (could have saved you a little typing there @aliaspooryorik 🙂
a
ha!
...doesn't show object creation 😛
😁 1
b
Good stuff guys, ty. @aliaspooryorik I'm still a little lost on how to test a component - in your example are you importing a component somewhere to start acting on it and asserting expected results from the action? (If you are I am missing it).
a
b
The importing and referencing and calling prod source code in a test is where I'm lost
a
b
Cool will check that out very helpful
a
there is also an example of how to run in the browser in that repo https://github.com/coldbox-modules/cbq/blob/main/tests/runner.cfm
f
Similarly here’s another simple example from that same repo I posted: we have a cfc
services/calc.cfc
with one function
add(x, y)
https://github.com/foundeo/cfml-ci-examples/blob/master/tests/specs/CalcTest.cfc
👆 1
👀 1
when you do
var calc = new services.calc()
CF is looking at the root of the site for a folder called services with a cfc file called calc.cfc OR and it will check this first, a mapping named services
a
If you haven't seen it - this is a good intro to CFML https://modern-cfml.ortusbooks.com/ (the modern version which isn't quite so
<cftag>
heavy!)
👍 1
b
Yeah that is great and I'm going through it
👍 1
a
and @foundeo is being modest but his site https://cfdocs.org/ is very useful
🙏 1
b
This was some great info. I'll dig into this and see if I can get some tests added. Happy to have found this community and looking forward to contributing what I can. In the meantime thanks for your help
a
You're welcome