Since I wanted to start this blog, I thought it would be good practice to write the software that runs it
using test-driven development. I've used a bit of TDD recently for additions to existing applications, but
I've not yet
started writing an application using it from beginning to end. I'm getting sick of
eating Italian microwaveable dinners when I have to maintain code. This is my chance to eat something else.
So, without further ado, we'll jump right in.
The first thing I did of course, was to create my directory structure. For the time being, we have:
xorblog/cfcs/src
and
xorblog/cfcs/tests
I like to keep the tests separate from the source. I don't have a reason behind it, other than it helps
keep me a bit organized.
Next, I thought about what a blog needs. We want to deliver items that have the highest business value first,
and move on to things that are lower on the value scale later. In doing this, we get a working application
sooner rather than later, and hence the blog can be used at the earliest possible moment in its development.
With that in mind, we probably shouldn't start with things like
Comments
or functionality
that lets us get included in places like
Technorati. Since
you need content to make anything else useful, I thought I'd start with that. Indeed, the
Post
is
the core part of a blog. Therefore, the first thing I did was create test_PostEntity.cfc under
xorblog/cfcs/tests.
Now, I'm using CFUnit for my tests, and this assumes you already have it set up. If you need help on
that, you can visit
CFUnit on SourceForge.
The first thing I do in test_PostEntity.cfc is write
test_hookup()
,
to make sure everything is working:
<cfcomponent extends="net.sourceforge.cfunit.framework.TestCase"
output="false"
name="test_PostEntity"
>
<cffunction name="test_hookup"
access="public"
returntype="void"
output="false"
>
<cfset assertEquals(expected=4, actual=2+2)>
</cffunction>
</cfcomponent>
Next, we need a way to see the status of and run our tests. For this we have test_runner.cfm, which
for the most part just copies what you'll find at the CFUnit site linked above:
<cfset testClasses = ArrayNew(1
)>
<cfset ArrayAppend(testClasses, "domains.xorblog.cfcs.tests.test_PostEntity"
)>
<!--- Add as many test classes as you would like to the array --->
<cfset suite = CreateObject("component"
, "globalcomponents.net.sourceforge.cfunit.framework.TestSuite"
).init( testClasses )>
<cfoutput>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<html>
<head>
<title>
Unit Tests for xorBlog</title>
</head>
<body>
<h1>
xorBlog Unit Tests</h1>
<cfscript>
createobject("component"
, "globalcomponents.net.sourceforge.cfunit.framework.TestRunner"
).run(suite,'');
</cfscript>
</body>
</html>
</cfoutput>
Finally, we run that page in a browser to make sure the test runs green - and it does.
Now that we have our test environment set up, we can start writing tests for our
PostEntity
that doesn't yet exist. (To be continued...)
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
There are no comments for this entry yet.
Leave a comment