当我尝试通过多个测试执行 phpUnit 时,有人失败并显示以下输出:
No matching handler found [...] Either the method was unexpected or its arguments matched no expected argument list for this method.
Run Code Online (Sandbox Code Playgroud)
但如果我单独执行测试,则没有错误地通过。如何在每次测试中强制使用不同的 Mockery 对象?
编辑:
如果我单独运行 phpunit,工作正常并且所有测试都通过,但是如果我使用包含 2 个类的目录...第一个方法通过,但第二个方法抛出此异常。
我在 AddCountryTest 类中有这个方法:
public function testAddCountry()
{
$name = 'test testAddCountryCommand';
$iso = 'derd';
$telPrefix = '+34 5';
$languageId = 1;
/* @var WtsCqrsRequest $wtsCqrsRequest */
$wtsCqrsRequest = \Mockery::mock('Wts\Country\App\Cqrs\Country\Request\AddCountryRequest')->makePartial();
/** @noinspection PhpUndefinedMethodInspection */
$wtsCqrsRequest
->shouldReceive('getIso')->andReturn($iso)
->shouldReceive('getTelPrefix')->andReturn($telPrefix)
->shouldReceive('getName')->andReturn($name)
->shouldReceive('getDefaultLanguageId')->andReturn($languageId)
;
$commandHandler = $this->getCommandHandler($wtsCqrsRequest);
/* @var Country $country */
$country = $commandHandler->handle($wtsCqrsRequest);
$this->assertEquals($country->getIso(), $iso);
}
Run Code Online (Sandbox Code Playgroud)
ReadCountryTest 类中的这个:
public function testReadCountry()
{
$name = 'test testAddCountryCommand';
$iso = 'zzz';
$telPrefix = '+34 5';
$languageId = 1;
$this->createCountry($name,$iso,$telPrefix,$languageId);
$iso = 'aaa';
$this->createCountry($name,$iso,$telPrefix,$languageId);
$iso = 'vvv';
$this->createCountry($name,$iso,$telPrefix,$languageId);
//Read
/* @var WtsCqrsRequest $wtsCqrsRequest */
$wtsCqrsRequest = \Mockery::mock('Wts\Country\App\Cqrs\Country\Request\ReadCountriesRequest')->makePartial();
$commandHandler = $this->getCommandHandler($wtsCqrsRequest);
/** @var WtsCqrsResponse $wtsCqrsResponse */
$wtsCqrsResponse = $commandHandler->handle($wtsCqrsRequest);
/* @var CountryDto[] countries */
$countries = $wtsCqrsResponse->getData();
/** @var CountryDto $countryRead */
$this->assertEquals(count($countries), 3);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 0
我也遇到了同样的问题,我知道这个问题已经有近 7 年历史了,但这是 Google 上出现的第一个结果,所以......
如果单个测试有效,但其中几个测试同时运行失败,这是因为您需要手动重置 Mockery 或实现 MockeryTestCase,
http://docs.mockery.io/en/latest/reference/phpunit_integration.html
之后,所有测试都将按预期进行。
| 归档时间: |
|
| 查看次数: |
2748 次 |
| 最近记录: |