代码之家  ›  专栏  ›  技术社区  ›  Bryan M.

PHPUnit、模拟接口和实例

  •  35
  • Bryan M.  · 技术社区  · 14 年前

    if ($instance instanceof Interface) {};
    

    然而,在PHPUnit中创建所述接口的模拟,我似乎无法通过该测试。

     // class name is Mock_Interface_431469d7, does not pass above check
     $instance = $this->getMock('Interface'); 
    

    我知道名为Interface的类与实现接口的类不同,但我不知道如何处理这个问题。

    我是否被迫模拟实现接口的具体类?这难道不会破坏使用接口实现可移植性的目的吗?

    2 回复  |  直到 14 年前
        1
  •  44
  •   martinvium    14 年前

    这对我很有用:

    $mock = $this->getMock('TestInterface');
    $this->assertTrue($mock instanceof TestInterface);
    

        2
  •  48
  •   Ben    11 年前

    从3.5.0开始还有断言

    例子:

    $this->assertInstanceOf('\Models\User', $this->userService->findById(1));
    
        3
  •  1
  •   Йося Гисем    5 年前

    例子:

    $this->assertInstanceOf(ResponseInterface::class, $signInResponse);