Does that sound like Doing The Right Thing, or is there some nifty trick out there that I'm missing?
The pieces you have can be unit tested individually if you break them out. Philosophically, you're doing three things:
- getting a key from a form (trivial to test in isolation)
- issuing a query based on that key (easy to test in isolation)
- expanding the value(s) returned from the query into a template (easy to test in isolation)
You've made a start on teasing these apart into separately testable routines. One possible next step is to isolate the logic that constructs the template. With the logic broken into separate routines, you've in a much better position to selectively replace substructure for testing purposes. (E.g., pull data back from in in-memory stub or repository, rather than querying the database).
On the "trick" side, here's one I've gotten mileage out of: Instead of hard-coding the template name, soft code it in a way that your test harness can change it to use a debugging template. The debugging template strips out everything unnecessary, leaving you with a result (and expanded template) that's easy to verify programmatically.
Are you posting in the right place? Check out Where do I post X? to know for sure.
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
Want more info? How to link or
or How to display code and escape characters
are good places to start.