Risky unit test when testing sendMail()

When you run unit tests for your frontend application, you will see that testSendEmailCorrectUser is returning ROK. After some debugging, I have discovered that test is risky because of mailer. Inside sendMail() method of PasswordResetRequestForm model we are sending email using mailer component. Inside testSendEmailCorrectUser test, we are expecting that sendMail() method return true (mail is successfully sent). Here is that line of code:

expect('email sent', $model->sendEmail())->true();

This line of code makes our test risky. Try to change expectation from true() to false().

expect('email sent', $model->sendEmail())->false();

Test will return ROK again! I guess this is because even if we are expecting that mail is sent it may fail for some reason, but we can not know that because we are not testing mailer component itself, we test our sendMail() method.

Client of my company do not tolerate risky tests, so I am writting this post to ask one question: how to turn this test green ?

If you run functional test: SignupCest.php, you can see that test will stop executing code after this:


    'username' => 'tester',

    'email' => 'tester.email@example.com',

    'password' => 'tester_password',


That is the last code that codeception will run, and it cause test to be risky.

I think Codeception fixed stopping on risky tests recently. Could be not yet released though.