Testing email component in CakePHP 2.1.x
14 Oct
Are you still sending yourself emails when you run test cases? I was.
Being relatively new to TDD this took a while to get my head around, but using “mocks” has proven to be really useful.
I’ll focus on the CakeEmail class in CakePHP 2.1.x as this proved most useful when writing test cases for Copify (we send _alot_ of emails!)
Here’s what one of our model methods looked like originally (before we had a test case).
When we started writing tests for other areas of the application which used this method, it fired an email.
To get round this, we had to mock the CakeEmail component. This also means you can test it is doing what it is supposed to be.
So to test this, we first need to make a change to the method itself:
Now, we can mock the CakeEmail class and test to ensure each part of our model method does what it should:
No comments yet