【发布时间】:2013-07-05 03:09:39
【问题描述】:
非常感谢有关我遇到的 Laravel 4 问题的一些帮助。
我正在测试控制器路由,特别是负责路由问卷响应的控制器。我正在测试以下场景:用户试图跳过一个问题,用户请求一个不存在的问题......等等。
到目前为止,我为所有场景编写的测试都使用 PHPunit 按预期工作。我目前正在编写的测试涉及多次$this->call() 或$this->client->request() 处决,这就是问题所在。如果我在单个测试方法中执行 $this->call() 或 $this->client->request() 太多次(具体为 2 次或更多次),我会在终端中收到 ErrorException:
{"error":{"type":"ErrorException","message":"未定义变量:header","file":"/Volumes/Dev HD/dmh/app/views/layouts/steps.php ","行":1}}
如果我将测试方法中$this->call() 或$this->client->request() 的数量减少到一个,一切正常,并且没有显示异常。我一直在使用以下代码:
代码
/**
* 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');
}
有什么想法吗?
我是否需要重置某些内容才能拨打多个电话?像这样打几个电话是不好的做法吗?有没有更好的方法来编写这个测试?任何想法将不胜感激。
非常感谢!
【问题讨论】:
标签: testing controller phpunit laravel laravel-4