Short answer: Think about what you want your forum code to do, then test that it does it :-)
Slightly more useful answer...
Before you write any code think about the requirement you're trying to meet, write a test for that requirement, then write some code to make the test pass.
For example, if your forum consists of a number of named boards you need to create them - so write a test for board creation:
isa_ok( Forum::Board->new(name => 'testboard'), 'Forum::Board' );
Run the test. It fails. Now go write the code to make a board. You know when you've finished because the test will pass.
Next you might need to get the name of an existing board, so write a test:
is( Forum::Board->new(name => 'testboard')->name, 'testboard');
Write code until test passes.
Repeat the test/code cycle until all the module does all you want it to do.
Never write code without a failing test. That way you build up your test suite as you go and end up with very good test coverage of your codebase.
If something looks too complicated to test break it down into smaller chunks. For example, you have a code that takes various parameters and passes them to a template to display to the user.
At the simplest level you could pass known parameters and look at the output and compare it with the output you want. Either as an exact string match, or by checking that the important bits are in the output string with a set of regexps. Gnarly.
However, you could also separate it into two different tests:
- Am I generating the correct parameters for the template
- Is the template generating the correct output from the parameters
Instead of testing both together, test each individually.
- Use a mock object for the template to make sure that the correct parameters are passed.
- Pass known parameters to the template to make sure that the correct output is produced.
This way you ensure that your unit tests really are unit tests - and you're only testing one component at a time.
I find that thinking "I can't test that" is usually a code smell. It indicates that there is a problem with my code and something is too tightly coupled to something else.
When you get to doing acceptance testing it's probably simplest to just drive the application with something like WWW::Mechanize. You might find Unit Testing Generated HTML of interest too.
Hopefully this makes some vague sort of sense :-) |