【问题标题】:Is MOCKING really useful apart from isolating dependent component testing?除了隔离依赖组件测试之外,MOCKING 真的有用吗?
【发布时间】:2025-12-13 10:55:01
【问题描述】:

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

为了在测试我的库时实现隔离,我使用了PHPUnit's 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")
        );
    }
}

testCreateTokenUrl()方法;它为 WPNonce 类创建一个模拟,并调用类方法 createTokenUrl() 。这是事情开始变得有趣的地方。

我正在定义像 with($this-&gt;equalTo("inpsyde.com?action=publish_postid=2")) 这样的输入参数,并且还明确地期望像 -&gt;will($this-&gt;returnValue('inpsyde.com?action=publish_postid=2&amp;token=297d58f6ae')); 那样的返回值

assertEquals() 方法会检查返回值是否是我在前面代码中设置的值。

这段代码帮助我单独测试createTokenUrl(),它不需要wordpress(在这种情况下我的库的依赖项)。我可以在没有安装 wordpress 的情况下成功通过测试。这很好,但如果模拟实际上是一个用参数和返回类型硬编码的虚拟测试,那么模拟项目依赖项的全部意义何在?

有没有一点我想念或我不明白的嘲笑点?

【问题讨论】:

  • 不确定*.com/questions/18811096/… 是否有帮助。
  • 现在写测试的方式确实没有意义。您不必担心测试不在您的库中的代码。只为您制作的代码创建测试。现在,如果您有自己的函数在调用 WPnonce 函数之前进行一些验证,那么像现在这样使用模拟是有意义的。但是现在测试没有做任何有用的事情。
  • @DirkScholten 你在说什么,而不是直接调用 WPnonce 函数,我应该将这些函数包装在另一个函数中,验证参数,最后用验证参数调用 WPnonce 函数?你是说在这种情况下嘲讽有意义吗?你不是这个意思吗?
  • @salimsaid 是的,这就是我的意思。但不要只是为了包装而包装它。仅当您确实想做一些验证(或其他事情)时才这样做。

标签: php wordpress unit-testing testing phpunit


【解决方案1】:

您必须区分被测系统 (SUT) 和 SUT 所依赖的组件(depended-on-components,DOC)。在您的情况下,SUT 似乎是方法createTokenUrl

Mocking 用于替换 DOC。但是,在您的测试中,您正在模拟 SUT(实际上,您正在创建 WPNonce 类的模拟,其中 createTokenUrl 是一个成员函数)。换句话说,您对$wpNonce-&gt;createTokenUrl 的调用将调用模拟方法——在测试中不会执行您要测试的真实方法的任何一行。这显然是没有意义的,但总体上不是嘲笑的问题。

【讨论】:

    最近更新 更多