Php 除了隔离依赖组件测试之外,模拟真的有用吗?

Php 除了隔离依赖组件测试之外,模拟真的有用吗?,php,wordpress,unit-testing,testing,phpunit,Php,Wordpress,Unit Testing,Testing,Phpunit,所以我正在为Wordpress开发一个安全库。基本上建立在wordpress核心组件的基础上,该组件名为 一切似乎都很好。 回到最佳实践,我已经在库中编写了一个单元测试套件。 由于我的库扩展了wordpress noces api,wordpress显然是我库中的一个依赖项 为了在测试我的库时实现隔离,我使用了PHPUnit的mockBuilder,因此我可以实例化依赖类,而不是实际实例化它,因为它调用外部依赖(wordpress nonce api)。 下面是我的测试课 <?php na

所以我正在为Wordpress开发一个安全库。基本上建立在wordpress核心组件的基础上,该组件名为
一切似乎都很好。
回到最佳实践,我已经在库中编写了一个单元测试套件。
由于我的库扩展了wordpress noces api,
wordpress
显然是我库中的一个依赖项

为了在测试我的库时实现隔离,我使用了
PHPUnit的mockBuilder
,因此我可以实例化依赖类,而不是实际实例化它,因为它调用外部依赖(
wordpress nonce api
)。
下面是我的测试课

<?php
namespace nonces;

/**
 * TestWpNonce
 * Test suite for wp-clean-nonces library
 *
 * Wp-clean-nonces is A clean OOP implementation of the Wordpress Nonce library.
 * Copyright (C) 2019  Salim Said.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For the complete terms of the GNU General Public License, please see this URL:
 * http://www.gnu.org/licenses/gpl-2.0.html

 * @category  NA
 * @package   NA
 * @author    Salim Said <saliimsaid@gmail.com>
 * @copyright 2019 Salim Said
 * @license   GPL-2.0+ <http://www.gnu.org/licenses/gpl-2.0.html>
 * @link      ****
 */

if(file_exists('../vendor/autoload.php'))
require_once '../vendor/autoload.php';

use PHPUnit\Framework\TestCase;
class TestWpNonce extends TestCase
{
    /**
     * This test will create a nonce for a url and verify if a correct
     * url is returned
     *
     * The method asserts for the result returned by createTokenUrl()
     * against a predefined expected return value containing a nonce
     * protected string url
     *
     * @return void
     */
    public function testCreateTokenUrl()
    {
        $wpNonce = $this->getMockBuilder(WPNonce::class)
            ->getMock();

        $wpNonce->expects($this->once())
            ->method('createTokenUrl')
            ->with($this->equalTo("inpsyde.com?action=publish_postid=2"))
            ->will($this->returnValue('inpsyde.com?action=publish_postid=2&token=297d58f6ae'));

        $this->assertEquals(
            "inpsyde.com?action=publish_postid=2&token=297d58f6ae",
            $wpNonce->createTokenUrl("inpsyde.com?action=publish_postid=2")
        );
    }
}

您必须区分被测系统(SUT)和SUT依赖的组件(依赖组件、文档)。在您的例子中,SUT似乎是方法
createTokenUrl


Mocking用于替换文档。但是,在测试中,您正在模拟SUT(实际上,您正在创建类
wpnance
的模拟,其中
createTokenUrl
是其成员函数)。换句话说,您对
$wpnice->createTokenUrl
的调用将调用模拟方法-测试中不会执行要测试的实际方法的任何一行。这显然没有意义,但一般来说不是一个嘲笑的问题。

不确定是否有帮助。现在编写测试的方式确实没有意义。您不应该担心测试库中没有的代码。仅为您编写的代码创建测试。现在,如果您有自己的函数,在调用wpnance函数之前执行一些验证,那么像现在这样使用mock是有意义的。但是现在测试没有做任何有用的事情。@DirkScholten您的意思是,我不应该直接调用wpnances函数,而是应该将这些函数包装到另一个函数中,验证参数,最后使用验证参数调用wpnances函数?你是说在这种情况下嘲笑是有意义的吗?“你不是这个意思吗?”萨利姆赛德是的,我就是这个意思。但不要只是为了包装而包装它。只有当你真的想做一些验证(或其他事情)的时候才这样做。