我想測驗這個功能:
public function handle(Request $request, Closure $next): mixed
{
$fields = [
'address1',
'address2',
'address3',
'city',
'country',
'countryName',
'email',
'firstname',
'lastname',
'mobile',
'phone',
'postcode',
'state',
'title'
];
foreach ($fields as $field) {
if ($request->get($field) !== null) {
return $this->scaActionMiddleware->handle($request, $next);
}
}
/** @var Response $response */
$response = $next($request);
return $response;
}
在我的單元測驗檔案中,我有以下功能:
/**
* @dataProvider dataProvider
*/
public function testHandleShouldBeOk(string $input, string $value): void
{
$request = $this->createMock(Request::class);
$response = $this->createMock(JsonResponse::class);
$next = (function () use ($response) {
return $response;
})(...);
$request->expects(self::any())
->method('get')
->with($input)
->willReturn($value);
$this->scaActionMiddleware->expects(self::once())
->method('handle')
->with($request, $next);
$this->scaActionUserMiddleware->handle($request, $next);
}
/**
* @return string[][]
*/
private function dataProvider(): array
{
return [
[
'address1',
'80 boulevard magenta'
],
[
'address2',
'22 rue du Commerce'
],
[
'address3',
'124 avenue des Champs'
],
[
'city',
'Paris'
],
[
'country',
'FR'
],
[
'countryName',
'France'
],
[
'email',
'[email protected]'
],
[
'firstname',
'Paul'
],
[
'lastname',
'Bar'
],
[
'mobile',
'0601589540'
],
[
'postcode',
'75010'
],
[
'state',
'state'
],
[
'address1',
'address1'
],
[
'title',
'Mr.'
]
];
}
最后,我有這些錯誤:
呼叫 0 次或多次時,方法名稱的預期失敗為“get” 呼叫 Illuminate\Http\Request::get('address1', null) 的引數 0:混合與預期值不匹配。斷言兩個字串相等時失敗。預期:'address2' 實際:'address1'
呼叫 0 次或多次時,方法名稱的預期失敗為“get” 呼叫 Illuminate\Http\Request::get('address1', null) 的引數 0:混合與預期值不匹配。斷言兩個字串相等時失敗。預期:'address3' 實際:'address1'
ETC...
我的印象是我的測驗函式 ( testHandleShouldBeOk) 只考慮dataProvider.
我遵循了這個檔案:https ://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html ,我也嘗試過設定yield,dataProvider但是我仍然有同樣的錯誤。
你能告訴我我哪里錯了嗎?
提前致謝。
編輯:感謝@Alister Bulman,我停止使用請求模擬。我使用一個真正的 Request 物件。
這是作業:
/**
* @dataProvider dataProvider
*
* @throws ConnectDbAccessException
* @throws JwtServiceException
* @throws ConfigServiceException
* @throws ScaServiceException
* @throws SerializerServiceException
*/
public function testHandleShouldBeOk(string $field, string $value, int $invoke): void
{
$request = Request::create('foo', 'POST', [$field => $value]);
$next = function () {
return $this->createMock(Response::class);
};
$this->scaActionMiddleware->expects(self::exactly($invoke))
->method('handle')
->with($request, $next);
$this->scaActionUserMiddleware->handle($request, $next);
}
/**
* @return array<int, array<int, int|string>>
*/
private function dataProvider(): array
{
return [
['address1', '80 boulevard magenta', 1],
['address2', '22 rue du Commerce', 1],
['address3', '124 avenue des Champs', 1],
['city', 'Paris', 1],
['country', 'FR', 1],
['countryName', 'France', 1],
['email', '[email protected]', 1],
['firstname', 'Paul', 1],
['lastname', 'Bar', 1],
['mobile', '0601589540', 1],
['postcode', '75010', 1],
['state', 'state', 1],
['address1', 'address1', 1],
['title', 'Mr.', 1],
['foo', '80 boulevard magenta', 0],
];
}
uj5u.com熱心網友回復:
您已經運行了 14 種不同的測驗 - 資料提供者的每個部分都有一個。您正在針對 14x 欄位測驗每個欄位,其中 13 個欄位不會設定。您可以對其進行測驗(設定所有欄位) - 或者讓您的測驗更接近預期的作業方式 - 對于它不知道的任何內容回傳 NULL。
在這里使用real Request而不是 mock 可能會有所幫助,因為它似乎會為它不知道的東西回傳 NULL。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523264.html
