【问题标题】:phpunit mockery No matching handler foundphpunit mockery 找不到匹配的处理程序
【发布时间】:2015-11-15 15:29:48
【问题描述】:

当我尝试使用多个测试执行 phpUnit 时,有人失败并显示以下输出:

No matching handler found [...] Either the method was unexpected or its arguments matched no expected argument list for this method.

但如果我单独执行测试,则可以通过而不会出错。我如何强制在每个测试中使用不同的 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);

}

这个在 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);

}

谢谢

【问题讨论】:

标签: phpunit mockery


【解决方案1】:

我遇到了同样的问题,我知道这个问题已经有将近 7 年的历史了,但这是谷歌上出现的第一个结果,所以......

如果单个测试有效,但同时运行多个测试失败,那是因为您需要手动重置 Mockery 或实现 MockeryTestCase,

http://docs.mockery.io/en/latest/reference/phpunit_integration.html

之后,所有测试都会按预期进行。

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 2014-08-16
    • 2013-03-29
    • 1970-01-01
    • 2015-06-23
    • 2013-05-24
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多