【问题标题】:twice() method doesn't work in Mockery PHPtwo() 方法在 Mockery PHP 中不起作用
【发布时间】:2014-12-04 07:18:48
【问题描述】:

我是 PHP Mockery 框架的新手。我有一个模拟函数 executePrepared($arg1, $arg2, arg3) 我调用了它两次但似乎没有工作并在 PHPUnit 命令行中给出以下错误:

Configuration read from C:\xampp\htdocs\DatabaseTesting\phpunit.xml

..←[31;1mE←[0m

Time: 47 ms, Memory: 3.25Mb

There was 1 error:


1) Test\Database\Table\TableTest::testinsertMany
 Mockery\Exception\NoMatchingExpectationException: No matching handler found for
 Mockery_0_Database_PGC::executePrepared(array(0=>'ClayPipe',1=>2000,2=>2100,3=>1
 ,4=>'2000-01-01',5=>'{"1":"1","2":6,"3":8,"4":10}',), "insert_assets", "\Databas
 e\Model\Asset"). Either the method was unexpected or its arguments matched no ex
 pected argument list for this method

我的测试功能如下:

 public function testinsertMany() {
      $this->PGCMock->shouldReceive('executePrepared')->twice()->withArgs(array(
[array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset'],
[array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'), 'insert_assets', '\Database\Model\Asset']))
->andReturn($expectedResult1);

$data1 = array('name'=>'Clay Pipe',
                    'hist_cost' => 2000,
                    'val_cost' => 2100,
                    'val_method' => 1,
                    'service_date' => '2000-01-01',
                    'tags' => '{"1":"1","2":6,"3":8,"4":10}'
                    );

    $data2 = array('name'=>'Main Street',
                    'hist_cost' => 1000,
                    'val_cost' => 1100,
                    'val_method' => 0,
                    'service_date' => '2000-02-01',
                    'tags' => '{"1":"1","2":6,"3":8,"4":10}'
                    ); 

    $actualResult = $this->tableMock->insertMany(array($data1,$data2));
}

我不明白这里有什么问题。我使用传递的参数调用模拟函数两次()的语法是否错误?任何人都可以在这里指导我吗?

【问题讨论】:

标签: php unit-testing phpunit mockery


【解决方案1】:

twice() 应在同一调用(包括参数)预计执行 2 次时使用。看起来你想检查 2 个连续的调用,每个调用都有不同的参数。

如果是这样,这将起作用:

$this->PGCMock
    ->shouldReceive('executePrepared')
    ->once()
    ->ordered()
    ->withArgs([
        array('Clay Pipe',2000,2100,1,'2000-01-01','{"1":"1","2":6,"3":8,"4":10}'),
        'insert_assets', '\Database\Model\Asset'
     ])
     ->andReturn($result1);
$this->PGCMock
    ->shouldReceive('executePrepared')
    ->once()
    ->ordered()
    ->withArgs([
         array('Main Street',1000,1100,0,'2000-02-01','{"1":"1","2":6,"3":8,"4":10}'),
         'insert_assets', '\Database\Model\Asset'            
     ])
     ->andReturn($result2);

【讨论】:

  • 谢谢@gontrollez!我的意图是只编写一次代码并使用不同的参数执行两次。有什么方法可以使用 times(n) 函数吗?很抱歉,我在官方文档中找不到简明的例子。
  • 不确定您的意思。但是如果 executePrepared 将被执行 2 次并且使用不同的参数,则必须单独配置每个调用。不是这样吗?
  • 是的,executePrepared 将使用不同的参数准确执行 2 次。如果我们必须单独配置每个呼叫,那么您提供的解决方案可以正常工作。我认为我们可以在一次通话中实现这一目标。无论如何,谢谢你的回答!
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 2013-03-24
相关资源
最近更新 更多