【问题标题】:PHPUnit: Mocking objects in 'Wrapper' objectsPHPUnit:在“包装器”对象中模拟对象
【发布时间】:2016-06-24 19:19:41
【问题描述】:

我不知道这是否有效,但我在文档或类似博客中找不到任何信息:

我想用类 B 的一些函数调用来测试我的类 A

class A {

    function foo() {

       B::doSomeThings();
    }
}

由于不使用真正的类B,我想模拟这个类。如果我对我的对象 A 进行单元测试,我只能找到模拟该对象的解决方案,例如:

$mockA = $this->getMockBuilder('\A')->setMethod('foo')->getMock();

$mockA->expects($this->once())->method('foo')->will(...)

是否可以只模拟 B

类中的函数 doSomeThings()

【问题讨论】:

    标签: php unit-testing mocking phpunit


    【解决方案1】:

    有一些解决方法

    http://miljar.github.io/blog/2014/01/29/phpunit-testing-static-calls/

    或者你可以移动到A函数中的其他方法,比如

    class A {
    
        function foo() {
    
         $this->doSomeThings();
        }
    
        function doSomeThings() {
           B::doSomeThings();
        }
    }
    

    在A类中模拟doSomeThings()函数

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2012-09-26
      • 1970-01-01
      • 2015-03-20
      • 2017-11-18
      • 2011-03-09
      • 2014-03-27
      • 1970-01-01
      • 2013-05-30
      相关资源
      最近更新 更多