除隔离相关组件测试外,MOCKING真的有用吗?

问题描述 投票:0回答:1

因此,我正在为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 <[email protected]>
 * @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->equalTo("inpsyde.com?action=publish_postid=2"))这样定义输入参数,并且也像->will($this->returnValue('inpsyde.com?action=publish_postid=2&token=297d58f6ae'));一样明确地期望返回值

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

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

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

php wordpress unit-testing testing phpunit
1个回答
0
投票

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

模拟用于替换DOC。但是,在测试中,您正在模拟SUT(实际上,您正在创建类WPNonce的模拟,其中createTokenUrl是成员函数)。换句话说,您对$wpNonce->createTokenUrl的调用将调用模拟方法-测试中不会执行要测试的真实方法的任何一行。这显然是没有道理的,但通常不是模拟问题。

© www.soinside.com 2019 - 2024. All rights reserved.