Test your models

I am a fan of test driven development (TDD) and so it is my goal to use this approach with CakePHP. In this post I will show you how to unit test a model. See http://wiki.cakephp.org/tutorials:testing_cake_apps for the configuration of the testing infrastructure.

Let us write a model for a contact form, where the user must enter an email address and a text. This is our model for the moment:

<?php
class Contactrequest extends AppModel
{
}
?>

What could go wrong with our model? Theoretically nothing, as we have not written anything. But in reality, we do not know if the framework works as expected, as there are almost no test cases for the framework available. So let us test if we can save our model.