PHPUnit在断言它在视图中看到值时给了我一个错误

时间:2019-06-29 11:12:14

标签: php laravel phpunit

当我运行具有2个断言(assertSee的功能测试时,即使我可以在{{中清楚地看到它},PHPUnit只能识别一个(title)而不识别description。 1}}作为从控制器传递到视图的值。

测试:

dd()

错误:

/** @test */
    public function a_user_can_view_a_project(){
        $this->withoutExceptionHandling();
        $project = factory('App\Project')->create();
        $this->get('/projects/'.$project->id)->assertSee('title')->assertSee('description');
    }

该段落是无法“看到”的描述。

dd():

Failed asserting that '<!doctype html>\n
<html lang="en">\n
<head>\n
    <meta charset="UTF-8">\n
    <meta name="viewport"\n
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">\n
    <meta http-equiv="X-UA-Compatible" content="ie=edge">\n
    <title>Document</title>\n
</head>\n
<body>\n
    <h3>Fugit molestias explicabo odio quis.</h3>\n
    <p>Quis aut consectetur quisquam a dolores voluptatibus. Quas quos quo iusto beatae sint voluptatem aspernatur. Molestiae repudiandae suscipit non vel. Ea quasi et eveniet.</p>\n
    Quis aut consectetur quisquam a dolores voluptatibus. Quas quos quo iusto beatae sint voluptatem aspernatur. Molestiae repudiandae suscipit non vel. Ea quasi et eveniet.\n
</body>\n
</html>\n
' contains "description".

1 个答案:

答案 0 :(得分:4)

要测试某个变量是否已传递到您的响应(您的情况下为视图),您需要使用

$response->assertViewHas("project",$project);

assertSee()方法仅检查作为参数传递的字符串是否为纯文本响应的子字符串。