我非常感谢有关我遇到的 Laravel 4 问题的帮助。
我正在测试控制器路由,特别是负责路由调查问卷响应的控制器。我正在测试以下场景:用户尝试跳过问题、用户请求不存在的问题......等等。
到目前为止,我为所有场景编写的测试都可以使用 PHPunit 按预期工作。我目前正在编写的测试涉及几个$this->call()
or $this->client->request()
处决,这就是事情崩溃的地方。如果我执行$this->call()
or $this->client->request()
在单个测试方法中多次(具体为 2 次或更多次)我在终端中收到 ErrorException:
{“错误”:{“类型”:“ErrorException”,“消息”:“未定义的变量:标头”,“文件”:“/Volumes/Dev HD/dmh/app/views/layouts/steps.php”,“行":1}}
如果我减少数量$this->call()
or $this->client->request()
在测试方法一中,一切正常,并且没有显示异常。我一直在使用以下代码:
Code
/**
* When the user skips questions they have not yet answered, the user
* should be redirected back to the first unanswered question.
*
* @return void
*/
public function testDiscoverSkipQuestions()
{
// Get the first question.
$domCrawler = $this->client->request('GET', 'style/discover');
// Answer the first question
$form = $domCrawler->selectButton("Next »")->form();
$form['question_1'] = 'A:0';
$response = $this->client->submit($form);
$this->assertRedirectedTo('style/discover/2');
// Get the 5th question.
$this->call('GET', 'style/discover/5');
// BROKEN
// Expect to be redirected to the 2nd question.
$this->assertRedirectedTo('style/discover/2');
$this->assertSessionHas('attention');
}
有任何想法吗?
我是否需要重置某些内容才能拨打多个电话?像这样拨打多个电话是不好的做法吗?有没有更好的方法来编写这个测试?任何想法将不胜感激。
非常感谢!
可能的解决方案:
Use $this->client->restart()
开始一个新的请求。
解释/详细信息:
好吧,这就是兔子洞:D
- Laravel
TestCase
延伸照明\基础\测试\测试用例 https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/TestCase.php
-
照明\基础\测试\测试用例 https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/TestCase.php uses 照明\基础\测试\客户端 https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/Client.php提出虚假请求。
-
照明\基础\测试\客户端 https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/Client.php延伸Symfony\Component\HttpKernel\Client https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Client.php
-
Symfony\Component\HttpKernel\Client https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Client.php扩展抽象类Symfony\Component\BrowserKit\ 客户端 https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/Client.php
- 抽象类Symfony\Component\BrowserKit\ 客户端 https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/Client.php有一个方法重新开始() https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/Client.php#L511
所以,我相信你的情况(我没有亲自测试过),你应该能够执行以下操作:
/**
* When the user skips questions they have not yet answered, the user
* should be redirected back to the first unanswered question.
*
* @return void
*/
public function testDiscoverSkipQuestions()
{
// Get the first question.
$domCrawler = $this->client->request('GET', 'style/discover');
// Answer the first question
$form = $domCrawler->selectButton("Next »")->form();
$form['question_1'] = 'A:0';
$response = $this->client->submit($form);
$this->assertRedirectedTo('style/discover/2');
// ******** Restart for new request ********
$this->client->restart();
// Get the 5th question.
$this->call('GET', 'style/discover/5');
// ******** SHOULD NOW WORK - call() is a proxy to $this->client->request(); ********
// Expect to be redirected to the 2nd question.
$this->assertRedirectedTo('style/discover/2');
$this->assertSessionHas('attention');
}
Notecall() 方法是使用代理$this->client->request() https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/TestCase.php#L56
希望有帮助!不要害怕深入研究代码继承,看看是否存在一种方便的方法来完成您需要的操作 - 更改通常已经存在:D
改进(?)
请注意,这些测试可能更倾向于“集成测试”而不是“单元测试”。与持续集成框架一起使用时,集成测试可能更适合。看这个问题 https://stackoverflow.com/questions/6511337/how-to-perform-integration-testing-in-php了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)