照亮\ Http \ Response - 属性"原创"包含字符串而不是View对象

时间:2016-10-02 06:55:25

标签: php unit-testing laravel-5.2 httpresponse

我从控制器的方法返回一个视图,如下所示:

return view('Main::pages.content-page', compact('content'));

视图呈现正确但是,当我读取响应对象时,我看到属性" original"响应对象是一个字符串,而它应该是视图对象。

Response {#385 ▼
    +original: """
    <!DOCTYPE html>\n
    <html lang="en">\n
        <head>\n
        <meta charset="utf-8">\n
        <meta http-equiv="X-UA-Compatible" content="IE=edge">\n
        <meta name="viewport" content="width=device-width, initial-scale=1">\n
        <meta name="author" content="">\n
        <meta name="csrf-token" content="2CQirHnZ7isCBRfkhOYkzPAWOzNIqJISrbb1mfDv">\n
        <meta name="description" content="">\n
        <meta name="keywords" content="">\n
          <title id="page_title">    D&eacute;couvrir\n </title>\n
        .........

这是预期的行为,因为我的测试实际上因为它而失败,因为在我的测试中基本上是在做

$this->get(route('main.page', ['content' => $content->slug]))
     ->assertResponseOk()
     ->assertViewHas('content', $content);

我遇到以下失败

F                                                                   1 / 1 (100%)

Time: 179 ms, Memory: 20.00MB

There was 1 failure:

1) IndexControllerTest::testGetPageMethod
The response was not a view.
Failed asserting that false is true.

查看assertViewHas()方法的结构,我们可以看到原因

/**
 * Assert that the response view has a given piece of bound data.
 *
 * @param  string|array  $key
 * @param  mixed  $value
 * @return $this
 */
public function assertViewHas($key, $value = null)
{
    if (is_array($key)) {
        return $this->assertViewHasAll($key);
    }

    if (! isset($this->response->original) || ! $this->response->original instanceof View) {
        return PHPUnit::assertTrue(false, 'The response was not a view.');
    }
    ......

条件! $this->response->original instanceof View失败,因为original是一个字符串,但应该是一个View对象。

所以我在这里迷路了。这是预期的行为,如果是这样,为什么在assertViewHas方法中的条件?

I am on Laravel Homestead version '3.0.2'
Laravel 5.2.45

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。在我的情况下,事实证明,响应在准备时收到了一个View对象,但是&#34;之后的#34;在返回之前触及响应的中间件类是将View对象转换为字符串。

您是否偶然使用genealabs/laravel-caffeine套餐?正是那个中间件引起了我的问题。通过将该软件包设置为不在测试环境中注册,我能够解决问题。