Answer the question
In order to leave comments, you need to log in
How to create a stub for a controller method in codecption?
Hello,
I have a console controller, a method is called from its action. I need to test what data is passed to this method. The test is like this (thanks @onqu):
$appConfig = require('unit/config/config.php');
$this->mockApplication($appConfig);
$controller = new \namespace\controllers\MyController('id', \Yii::$app);
$result = $controller->run('my-action');
$this->assertEquals(1, $result);
Answer the question
In order to leave comments, you need to log in
In this case, mock is more convenient, but before that it is better to check that the method is being called at all. Stub is good for a complete replacement of something.
// аргументы конструктора MyController
$constructorArgs = [
'id',
Yii::$app,
];
// заменяемые методы и свойства MyController
$methods = [
// метод, который тестируем
// подойдет любой тип callable
'methodToTest' => function() {
$args = func_get_args();
... тестируем аргументы
},
];
$controllerMock = \Codeception\Util\Stub::construct(
'\namespace\controllers\MyController',
$constructorArgs,
$methods
);
$controllerMock->run('action', []);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question