【问题标题】:Codeception unit testing php. Check that a function has been called with a certain parameterCodeception 单元测试 php.ini检查是否已使用特定参数调用函数
【发布时间】:2020-10-14 21:39:45
【问题描述】:

我试图为这个简单的问题找到解决方案。我需要检查是否已使用某个参数调用了一个函数。但我可以检查一下,一个函数已被调用并返回一些参数。我在代码分离文档中搜索了很长时间。有一些方法可以解决这个问题吗?

   public function testSomething()
   {
      $conversation = $this->make('Class', [
         'ask' => \Codeception\Stub\Expected::once('this string will be returned')
      ]);
      $conversation->run();
   }

【问题讨论】:

    标签: php unit-testing testing integration-testing codeception


    【解决方案1】:

    通过 codeception,我正在使用类似这样的东西:

    $conversation = Stub::make(MyClass::class,
                [
                    'ask' => Stub\Expected::exactly(1, function ($params)  {
                        $this->assertEquals('yourFirstParam', $params[0]);
    
                    })
                ]
            );
    

    【讨论】:

    • 谢谢你的回答,我试试看!
    【解决方案2】:

    我找到了解决办法。它是 PHPUnit 语法,但它有效!我们测试该方法 ask() 在第一次调用时使用了 2 个参数(“第一个参数”、“第二个参数”)。也许有人有更优雅的解决方案?

      public function testSomething()
       {
          $conversation = $this->make('Class', [
             'ask' => \Codeception\Stub\Expected::once()
          ]); 
    
      $conversation->expects($this->exactly(1))
         ->method('ask')
         ->with('first param', 'second param');
    
          $conversation->run();
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2018-06-11
      • 2018-11-21
      相关资源
      最近更新 更多