我有一个问题,我不知道如何说,当x被调用时,它希望y至少被调用一次并且我不在乎你做什么,只要它被称为
因此,如果我们看一下这个例子:
public static function createAction($params) {
$postParams = $params->request()->post();
// Controller logic here ...
$params->redirect('/signin');
}
我想在我的测试中做什么,如下所示 - 我希望redirect()
以'/singin'
的参数调用一次。
我的测试看起来像:
<?php
use \GP\Models\User;
use Slim\Environment;
use \GP\Controllers\UserController;
class UserControllerTest extends PHPUnit_Framework_TestCase {
protected $em;
protected $app;
public function setUp() {
$this->em = getEntityManager(true);
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
$mdFactory = $this->em->getMetadataFactory();
$tool->dropSchema($mdFactory->getallMetadata());
$tool->createSchema($mdFactory->getallMetadata());
parent::setUp();
$_SESSION = array();
$this->app = new \Slim\Slim(array('mode' => 'testing'));
}
public function tearDown() {
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
$mdFactory = $this->em->getMetadataFactory();
$tool->dropSchema($mdFactory->getallMetadata());
parent::tearDown();
}
public function testUserCreate() {
Environment::mock(array(
'REQUEST_METHOD' => 'POST',
'slim.input' => 'firstname=user&lastname=somethingelse&username=admin&email=a@gmail.com&password=1234567890&repassword=1234567890'
));
UserController::createAction($this->app);
$user = $this->em->getRepository('\GP\Models\User')
->findBy(array('user_name' => 'admin'));
$this->assertNotEmpty($user);
}
}
当我跑步时,我得到了:
$ phpunit
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
Configuration read from /var/www/html/Grab-Project/phpunit.xml
....E
Time: 13.45 seconds, Memory: 21.25Mb
There was 1 error:
1) UserControllerTest::testUserCreate
Slim\Exception\Stop:
/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1022
/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1042
/var/www/html/Grab-Project/vendor/slim/slim/Slim/Slim.php:1105
/var/www/html/Grab-Project/app/Controllers/UserController.php:73
/var/www/html/Grab-Project/tests/app/controllers/UserControllerTest.php:40
FAILURES!
Tests: 5, Assertions: 8, Errors: 1.
尝试调用重定向时会发生此问题。这是因为我没有在我的测试中指定slim应该重定向,而是我想要存根该方法然后说,我希望在数据库中找到这个用户,就像我一样,我期望这个方法与这个参数是叫了一次。
我如何在rails中执行此操作,但阅读documentation让我感到困惑......
答案 0 :(得分:1)
如果$ params-&gt;重定向(&#39; / signin&#39;);是它打破的线我认为你应该能够解决这个问题。
在设置中:
$this->app = $this->getMock(
'\Slim\Slim',
array('redirect'),
array(array('mode' => 'testing'))
);
array(array(故意,因为它是一个构造函数参数数组,你的constuctor看起来像是想要一个数组作为第一个参数。
然后,您可以添加到测试功能中。
$this->app->expects($this->once())->method('redirect');