【问题标题】:PHPUnit assert method not equal toPHPUnit 断言方法不等于
【发布时间】:2013-09-16 16:50:25
【问题描述】:

我有一个使用 ServiceB 的 ClassA。在某种情况下,ClassA 应该最终不会调用 ServiceB 的任何方法,除了一个。我现在想对此进行测试并验证确实没有调用其他方法。

这可以按如下方式完成:

$classA->expects( $this->once() )->method( 'first_method' );
$classA->expects( $this->never() )->method( 'second_method' );
$classA->expects( $this->never() )->method( 'third_method' );
...

有没有一种方法可以简单地声明“不应该首先在此对象上调用任何方法”,而不必为每个方法指定一个限制?

我目前有这种可行的方法,但我的测试用例依赖于 PHPUnit 的两个内部类,我宁愿避免。

$schema->expects( $this->never() )
            ->method( new PHPUnit_Framework_Constraint_Not( new PHPUnit_Framework_Constraint_IsEqual( 'addField' ) ) );

有没有办法使用流畅的界面来做同样的事情?

【问题讨论】:

    标签: php unit-testing testing mocking phpunit


    【解决方案1】:

    是的,有 :) 试试这个:

    $classA->expects($this->once())->method('first_method');
    $classA->expects($this->never())
        ->method($this->logicalNot($this->equalTo('first_method')));
    

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 2019-11-15
      • 2016-10-04
      • 2016-08-21
      • 2013-09-15
      • 2013-10-01
      • 1970-01-01
      • 2016-09-14
      相关资源
      最近更新 更多