【问题标题】:PHPUnit assert method called with each value使用每个值调用 PHPUnit 断言方法
【发布时间】:2013-10-01 12:51:21
【问题描述】:

我有一个方法应该使用它以某种方式获得的数组,然后为每个元素调用另一个方法(我们称之为 fooBar),将元素作为参数传递给该方法。

这是我目前拥有并且有效的代码:

    foreach ( $names as $index => $name ) {
        $parser->expects( $this->at( $index ) )
            ->method( 'setFunctionHook' )
            ->with(
                $this->equalTo( $name ),
                $this->isType( 'callable' )
            );
    }

虽然这很冗长。有没有更短的方法?

【问题讨论】:

    标签: unit-testing testing mocking phpunit assertions


    【解决方案1】:

    假设您知道测试中的预期值:

    foreach($expected_values as $value){
        $myMock->expects( $this->once())
            ->method('fooBar')
            ->with($value);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2016-10-04
      • 2013-09-15
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多