I am trying to test database records retrieving. My test looks like:
use yii\db\Exception;
class UserTest extends Unit
{
protected $tester;
private $_user;
public function _before()
{
$this->_user = new User();
}
public function testRetrievingFALSE()
{
$this->expectException(Exception::class, function(){
$this->_user->retrieveRecords();
});
}
}
I saw the expectException() method in the documentation. My model method looks like this:
public function retrieveRecords()
{
$out = ArrayHelper::map(User::find()->all(), 'id', 'username');
if($out)
return $out;
else
throw new Exception('No records', 'Still no records in db');
}
What am I doing wrong in this scenario?
In terminal:
Frontend\tests.unit Tests (1) ------------------------------------------------------------------------------------------
x UserTest: Retrieving false (0.02s)
------------------------------------------------------------------------------------------------------------------------
Time: 532 ms, Memory: 10.00MB
There was 1 failure:
---------
1) UserTest: Retrieving false
Test tests\unit\models\UserTest.php:testRetrievingFALSE
Failed asserting that exception of type "yii\db\Exception" is thrown.
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.