在我的测试中不能使用assertViewHas方法

问题描述 投票:0回答:1

我正在学习Laravel测试,我希望使用Laravel Framework Assertions

public function __construct()
{
    $this->mock = Mockery::mock("Eloquent", "Post");
}

public function testIndex()
{
    $this->mock
       ->shouldReceive('all')
       ->once()
       ->andReturn('foo');

    $this->app->instance('Post', $this->mock);
    $this->call('GET', 'posts');
    $this->assertViewHas('posts'); // This method seem can't be find 
}

似乎assertViewHas方法不存在,因为我的IDE无法自动完成该方法它无法找到它,但是当我在Laravel 5.5 API中搜索时,我在assertViewHas类中的TestResponse类中的Illuminate/Foundation/Testing方法中找到该方法。我怎么解决这个问题。

另一个问题是,在这个测试中,我使用嘲弄来模拟我的Post模型,但是当我在我的终端上运行vendor\bin\phpunitin时出现了一些错误。

PHP unit Error

我没有找到array_merge()在我的测试中使用的位置

php laravel-5 phpunit laravel-5.5 mockery
1个回答
2
投票

代替:

$this->call('GET', 'posts');
$this->assertViewHas('posts'); // This method seem can't be find 

你应该使用

$response = $this->call('GET', 'posts');
$response->assertViewHas('posts');

Laravel 5.4中更改了运行测试的默认方式。有关详细信息,请参阅Upgrade guide - Testing section

© www.soinside.com 2019 - 2024. All rights reserved.