testing - Laravel 4 Controller 测试 - 太多 $this->call() 后出现 ErrorException - 为什么?

标签 testing controller phpunit laravel laravel-4

对于我遇到的 Laravel 4 问题,如果能提供一些帮助,我将不胜感激。

我正在测试 Controller 路由,特别是负责路由问卷响应的 Controller 。我正在测试以下场景:用户试图跳过一个问题,用户请求一个不存在的问题......等等。

到目前为止,我为所有场景编写的测试都可以使用 PHPunit 按预期工作。我目前正在编写的测试涉及多个 $this->call()$this->client->request() 执行,这就是问题所在。如果我在单个测试中执行 $this->call()$this->client->request() 太多次(具体来说是 2 次或更多次)方法我在终端中得到一个 ErrorException:

{"error":{"type":"ErrorException","message":"Undefined variable: header","file":"/Volumes/Dev HD/dmh/app/views/layouts/steps.php","line":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');
}

有什么想法吗?

我是否需要重置一些东西才能调用多个电话?像这样打几个电话是不好的做法吗?有没有更好的方法来编写这个测试?任何想法将不胜感激。

非常感谢!

最佳答案

可能的解决方案:

使用$this->client->restart()开始一个新的请求。

解释/细节:

好吧,这里是兔子洞 :D

  1. Laravel TestCase 扩展 Illuminate\Foundation\Testing\TestCase
  2. > Illuminate\Foundation\Testing\TestCase使用 Illuminate\Foundation\Testing\Client提出虚假请求。
  3. > Illuminate\Foundation\Testing\Client延伸Symfony\Component\HttpKernel\Client
  4. > Symfony\Component\HttpKernel\Client扩展抽象类 Symfony\Component\BrowserKit\ Client
  5. 抽象类 Symfony\Component\BrowserKit\ Client有一个方法 restart()

所以,我相信你的情况(我没有亲自测试过),你应该能够做到以下几点:

/**
 * 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');
}

注意 call() 方法是a proxy to using $this->client->request()

希望对您有所帮助!不要害怕深入研究代码继承,看看是否存在一种方便的方法来做你需要的事情——改变通常已经存在了:D

改进(?)

请注意,这些测试可能更倾向于“集成测试”而不是“单元测试”。当与持续集成框架一起使用时,集成测试可能更合适。参见 this question了解更多信息。

关于testing - Laravel 4 Controller 测试 - 太多 $this->call() 后出现 ErrorException - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17515563/

相关文章:

ruby-on-rails - 使用带有 Resque 后台作业的 Controller 方法

php - CodeIgniter/胖模型/瘦 Controller

rest - PHPUnit - REST API 测试

php - 我如何在 laravel 中编写函数返回 json 数据的测试

testing - 机器人框架: exception handling

java - 在运行测试之前创建测试的 JUnit Test Runner

c# - 从 Asp.Net MVC View 转到下一条记录

json - "Trying to get property of non-object"在 Symfony 4.1 上执行 JSON 请求测试时

java - Junit中如何使用基类@BeforeMethod对所有测试用例执行for循环

mysql - Bash Linux - 测试 mysql 命令是否成功执行