Someone said that unit tests should tell a story. This approach can help you write tests that make sense in terms of coding the application. Write descriptive names for each test method, for example:
function testUpdatingInsecurePasswordShouldFail() { $data = array('User' => array( 'password' => 'password' )); $result = $this->User->updatePassword($data); $this->assertFalse($result); $data = array('User' => array( 'password' => '' )); $result = $this->User->updatePassword($data); $this->assertFalse($result); }
By telling the βhistoryβ of insecure passwords, you can then write the model code so that the new test passes. Another example:
function testUpdatingStrongPasswordShouldSucceed() { $data = array('User' => array( // forget about hashing for the moment 'password' => 'battery hoarse collect maple' )); $this->User->updatePassword($data); $result = $this->User->find('count', array( 'conditions' => array( // making some assumptions about the test data here 'User.username' => 'test_user1', 'User.password' => 'battery hoarse collect maple', ), ); $this->assertEqual($result, 1); }
Please note that we are doing a bit more work to make sure that the update works correctly. When the testing environment begins to collect errors and regressions, you will be glad that you made additional efforts.
One of the advantages of good descriptive test names is that we can now use the cake test --testdox to display the results in plain English:
[x] Updating insecure password should fail [x] Updating strong password should succeed
source share