php - Laravel - phpunit 数组断言

标签 php laravel phpunit

我正在尝试编写一个 phpunit 测试,以便我可以测试我是否得到了正确的断言,我目前拥有的这个测试正在通过并且按预期工作。

/**
@test
*/
  $filmInfo = $this->postRequest();

        $this->assertFilm($this->requestData, $filmInfo['id']);

        $film = Film::findOrFail($filmInfo['id']);

        $this->assertEquals('LOTR', $filmInfo['name']);
        $this->assertEquals('Film about a ring', $filmInfo['description']);
        $this->assertEquals('Fantasy', $filmInfo['genre']['main']);
        $this->assertEquals('Adventure', $filmInfo['genre']['sub']);
    }

这是它所引用的请求数据数组:

    private function requestData(): array
    {
        return [
            'name' => 'LOTR',
            'description' => 'Film about a ring',
            'main' => 'Fantasy',
            'sub' => 'Adventure',
        ];
    }

这个测试工作正常并且通过了,但我想在一个断言中测试它,如下所示:

  $this->assertEquals([
            'name' => 'LOTR',
            'description' => 'Film about a ring',
            'genre' => [
                'main' => 'Fantasy',
                'sub' => 'Adventure'
            ]
            ,
        ], $filmInfo);

但是我一直收到一个错误,我断言的两个数组不匹配,你们知道是什么原因导致的吗?

最佳答案

就像 PHPUnit 所说的那样,您的数组不匹配。您的数组应与 $filmInfo 数组中的所有值匹配。

从您的代码中,我们可以猜测您没有比较id。也许你可以试试这个代码:

$this->assertEquals(array_merge($filmInfo, [
    'name' => 'LOTR',
    'description' => 'Film about a ring',
    'genre' => [
        'main' => 'Fantasy',
        'sub' => 'Adventure'
    ],
]), $filmInfo);

关于php - Laravel - phpunit 数组断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474875/

相关文章:

php - Smarty 与 JQuery 的冲突

php - Laravel 7 : Redirect to different logins on different guards

Symfony2 phpunit 给出 503 错误

php - Wamp、MYSQL 和 ON DUPLICATE KEY UPDATE 不影响正确的行数?

php - 在非对象 MySQLi 上调用成员函数 bind_param()

php - Laravel Eloquent wherePivot 方法返回未找到列 'pivot'

php - 使用 composer 将 github 包安装到 Laravel 项目中

rest - 使用 phpunit 和doctrine 测试 symfony Restful api

phpunit - 无法使用 CodeCoverage 和 Composer 重新声明类错误

php - 对未定义函数 geoip_open() 的 fatal error 调用