【问题标题】:PHPUnit : mock an Interface and super class at the same timePHPUnit:同时模拟一个接口和超类
【发布时间】:2017-07-26 01:32:12
【问题描述】:

我有一个界面:

interface MyInterface {

   const SOME_CONSTANT='hi';

   function method(): void;
}

还有一个超类:

class MyClass {

   private $id;

   function method1(){
       //do something
   }
}

我需要一个实现接口并扩展超类的mock,即mock需要同时属于MyInterface和MyClass类型。

TestCase::createMock 方法可以只模拟一个类,所以我希望看看是否可以使用 PHPUnit 6 获得我需要的模拟。

【问题讨论】:

    标签: php unit-testing mocking phpunit


    【解决方案1】:

    你可以在这里使用一些预言。

    class ClassAndInterfaceTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @test
         */
        function classAndIface ()
        {
            $myclass_instance = $this->prophesize (MyClass::class)
                ->willImplement (MyInterface::class)->reveal ();
    
            $this->a ($myclass_instance);
            $this->b ($myclass_instance);
        }
    
        function a (MyInterface $i)
        {
        }
    
        function b (MyClass $i)
        {
        }
    }
    

    【讨论】:

    • 请不要在调用(和定义)括号之间添加空格。谢谢;)
    猜你喜欢
    • 2014-04-03
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2021-06-23
    相关资源
    最近更新 更多