【问题标题】:Laravel - phpunit array assertionLaravel - phpunit 数组断言
【发布时间】:2020-02-16 20:51:10
【问题描述】:

我正在尝试编写一个 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);

但我不断收到一个错误,即我断言的 2 个数组不匹配,你们知道是什么原因造成的吗?

【问题讨论】:

  • $filmInfo 中是否有更多数据?你可以转储/发布它吗?

标签: php laravel phpunit


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 2016-09-14
    • 2018-08-31
    • 2013-09-09
    • 2013-07-26
    • 2011-08-06
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多