Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当我使用mockry时,Laravel 5“AssertReposeOK”失败(得到500个错误)_Php_Unit Testing_Laravel 5_Mockery - Fatal编程技术网

Php 当我使用mockry时,Laravel 5“AssertReposeOK”失败(得到500个错误)

Php 当我使用mockry时,Laravel 5“AssertReposeOK”失败(得到500个错误),php,unit-testing,laravel-5,mockery,Php,Unit Testing,Laravel 5,Mockery,我正在尝试使用mockry来编写单元测试,它正在工作。然而,有一件事是行不通的 我已经寻找答案好几天了,但我什么也没找到,所以事情是这样的。首先,我将列出一个摘要版本,然后列出所有代码 这不起作用: 运行phpunit时,最后一行代码给出了以下错误: Expected status code 200, got 500 mock类和assert方法应该可以正常工作。我通过调用错误的方法或从控制器中删除调用来检查它,尽管当出现响应错误时,模拟似乎没有任何效果,因为我只得到响应错误 如果我删除了模拟

我正在尝试使用mockry来编写单元测试,它正在工作。然而,有一件事是行不通的

我已经寻找答案好几天了,但我什么也没找到,所以事情是这样的。首先,我将列出一个摘要版本,然后列出所有代码

这不起作用:

运行phpunit时,最后一行代码给出了以下错误:

Expected status code 200, got 500
mock类和assert方法应该可以正常工作。我通过调用错误的方法或从控制器中删除调用来检查它,尽管当出现响应错误时,模拟似乎没有任何效果,因为我只得到响应错误

如果我删除了模拟代码,只留下响应,它就可以工作了,我得到的状态代码是200而不是500,所以我一定是遗漏了什么

我一直在关注一些关于模拟的文章,其中包括模拟后的两条回复行

因此,这似乎是非常基本的东西,工作没有问题。我指的其中一篇文章是这篇

我也尝试过:

写入“$this->app->instance”而不是“app::instance”,并将其置于“shouldReceive”之前,不会产生任何效果。 在tearDown方法中包括“parent::tearDown”。 猜猜看?怎么了

顺便说一下,我目前正在做一项工作:

我在没有检查“assertResponseOk”的情况下进行了模拟测试,并检查了模拟是否如我前面所说的那样工作。 我有另一种方法来检查响应,如果视图正在获取数据等等,而不使用mock。 下面是失败的“完整”代码。我还没有包括存储库代码。它可以正常工作,视图可以正确显示数据

测试代码:

控制器代码:


我相信每一次模拟都应该返回至少一个空数组,因为否则您将为数组分配null。

如果没有视图代码,我不能确定,但是,我非常确定您的代码正在崩溃,因为您的movies.index视图要求某些$data数组键不能为null。我建议采取以下措施之一:

规避视图逻辑 来自存储库的模拟响应
上面的代码没有经过测试,但您应该了解这一点

以各种方式检查,也就是说,返回不同的东西。此外,在我遵循的教程/示例中,没有提到这样的事情。谢谢你,我刚读过。我已经搬到别的地方去了,不过我要试试看。非常感谢。
Expected status code 200, got 500
$this->call('GET', 'movies');
$this->assertResponseOk();
class MovieControllerTest extends TestCase {

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        Mockery::close();
    }

    public function testIndexCallsRepository()
    {
        $mock = Mockery::mock('App\Repositories\Movie\IMovieRepository');

        $mock->shouldReceive('getAll')->once();
        $mock->shouldReceive('getGenres')->once();
        $mock->shouldReceive('getCountries')->once();
        $mock->shouldReceive('getFormats')->once();
        $mock->shouldReceive('getEncodigs')->once();
        $mock->shouldReceive('getUbications')->once();

        App::instance('App\Repositories\Movie\IMovieRepository', $mock);

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

        $this->assertResponseOk();
    }

    public function testIndexResponseIsOkAndViewHasAllTheData()
    {
        $response = $this->call('GET', 'movies');

        $this->assertResponseOk();

        $this->assertViewHas('movies');
        $this->assertViewHas('genre_options');
        $this->assertViewHas('country_options');
        $this->assertViewHas('format_options');
        $this->assertViewHas('encoding_options');
        $this->assertViewHas('ubication_options');

        $movies     = $response->original->getData()['movies'];
        $genres     = $response->original->getData()['genre_options'];
        $countries  = $response->original->getData()['country_options'];
        $formats    = $response->original->getData()['format_options'];
        $encodings  = $response->original->getData()['encoding_options'];
        $ubications = $response->original->getData()['ubication_options'];

        $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', 
            $movies);
        $this->assertInternalType('array', $genres);
        $this->assertInternalType('array', $countries);
        $this->assertInternalType('array', $formats);
        $this->assertInternalType('array', $encodings);
        $this->assertInternalType('array', $ubications); 
    }
}
<?php namespace App\Http\Controllers;

use App\Repositories\Movie\IMovieRepository;

class MovieController extends Controller {

    protected $movie;

    public function __construct(IMovieRepository $movie)
    {
        $this->movie = $movie;
    }

    public function index()
    {
        $data['movies']            = $this->movie->getAll();
        $data['genre_options']     = $this->movie->getGenres();
        $data['country_options']   = $this->movie->getCountries();
        $data['format_options']    = $this->movie->getFormats();
        $data['encoding_options']  = $this->movie->getEncodings();
        $data['ubication_options'] = $this->movie->getUbications();

        return view('movies.index', $data);
    }
}
Route::get('movies', 'MovieController@index');
public function testIndexCallsRepository()
{
    $mock = Mockery::mock('App\Repositories\Movie\IMovieRepository');

    $mock->shouldReceive('getAll')->once()->andReturn('all');
    $mock->shouldReceive('getGenres')->once()->andReturn('genres');
    $mock->shouldReceive('getCountries')->once()->andReturn('countries');
    $mock->shouldReceive('getFormats')->once()->andReturn('formats');
    $mock->shouldReceive('getEncodigs')->once()->andReturn('encodings');
    $mock->shouldReceive('getUbications')->once()->andReturn('ubications');

    App::instance('App\Repositories\Movie\IMovieRepository', $mock);

    $expectedViewData = array(
         'movies' => 'movies',
         'genre_options' => 'genres',
         'country_options' => 'countries',
         'format_options' => 'formats',
         'encoding_options' => 'encodings',
         'ubication_options' => 'ubications',
    );

    View::shouldReceive('make')->once()->with('movies.index', $expectedViewData)->andReturn('compiled view'); 

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

    $this->assertResponseOk();
}
public function testIndexCallsRepository()
{
    $mock = Mockery::mock('App\Repositories\Movie\IMovieRepository');

    $mock->shouldReceive('getAll')->once()->andReturn(array('all'));
    $mock->shouldReceive('getGenres')->once()->andReturn(array('genres'));
    $mock->shouldReceive('getCountries')->once()->andReturn(array('countries'));
    $mock->shouldReceive('getFormats')->once()->andReturn(array('formats'));
    $mock->shouldReceive('getEncodigs')->once()->andReturn(array('encodings'));
    $mock->shouldReceive('getUbications')->once()->andReturn(array('ubications'));

    App::instance('App\Repositories\Movie\IMovieRepository', $mock);

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

    $this->assertResponseOk();
}