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