php - 我应该如何测试这个方法?

标签 php testing phpunit

我最近一直在尝试在我的项目中采用 TDD 方法,但我很难知道如何测试某些代码片段。我已经阅读了很多关于这个主题的文章,但我很难接受并将其付诸实践。既然如此,我将发布我的方法并询问您将如何尝试对其进行测试。

public function simulate(){
    while (!isComplete()) {
        if ($this->currentOuts == 3) {
            advanceInning();
        } else {
            $batter = getBatter();
            $pitcher = getPitcher();
            $atBat = new AtBat($batter, $pitcher);
            $result = $atBat->simulate();
            handleAtBatResult();
        }
    }
}

假设模拟中的所有函数调用都得到了正确的测试。真的还有什么可以测试的吗?也许调用了某些函数?缺乏明显的测试(至少对我而言)是否表明存在设计问题?

最佳答案

当开始使用 TDD 时,我最终问了你在这里问的相同问题。经过一些研究,以及数周的单元测试等工作后,我想出了两个术语; “流量测试”和“模块测试”。

模块测试:作为工程师,我们应该努力遵循 DRY(不要重复自己)原则,因此,我们最终会得到抽象的代码片段,这些代码片段会被推送到应用程序的最低层,因此它们可以在任何地方使用。这些代码片段,无论是类的方法还是独立的函数,都应该是原子可测试的,这意味着对任何其他模块、函数等的依赖性最低。显然,当你处理包含多个模块的方法/函数时,这是可以避免的模块,但这是流量测试发挥作用的地方。

流测试:由于我们所有的基本模块都处于可测试状态,我们还需要能够在与现实世界需求相称的场景中测试它们。为了正确地进行流量测试,我们需要建立我所说的“known goods”。这意味着我们构建的数据反射(reflect)了流程测试中模块的返回值,因此我们可以将它们与 API 生成的值进行比较。

为了帮助更好地展示这些想法,这是我为测试缓存 api 所做的流程测试(添加了一些额外的评论以更好地解释):

<?php

class HobisTest_Api_Flow_CacheTest extends PHPUnit_Framework_TestCase
{
    // Setting some constants so it's easier to construct known goods
    const TEST_EXPIRY       = 30;
    const TEST_KEY_PREFIX   = 'test';
    const TEST_VALUE        = 'brown chicken, brown cow';

    //-----
    // Support methods
    //-----
    protected $object;
    protected $randomNumber;

    // Here we generate a known good key, this allows us to test that the api internal workings generate what we expect
    protected function getKnownGoodKey()
    {
        return self::TEST_KEY_PREFIX . Hobis_Api_Cache_Key::SEPARATOR . $this->getRandomNumber() . Hobis_Api_Cache_Key::SEPARATOR . '1';
    }

    protected function getObject()
    {
        return $this->object;
    }

    protected function getRandomNumber()
    {
        return $this->randomNumber;
    }
    //-----

    //-----
    // Setup and teardown
    //-----

    // You will want to add setup and teardown functions to your test classes
    //  These allow you to reference items for EVERY test within the current class
    //  While ensuring they are not carried over from one test to the other
    //  Basically a clean slate for every test
    public function setUp()
    {
        $this->object       = $this->getMock('Hobis_PhpUnit_DefaultTestObject');
        $this->randomNumber = mt_rand();
    }

    public function tearDown()
    {
        unset(
            $this->object,
            $this->randomNumber
        );
    }
    //-----

    //-----
    // Test methods
    //-----

    // The actual test method
    public function testCache()
    {
        // Configure object 
        //  Setting up so any references to $this->getId() will return 1
        //  If you look in the getKnownGoodKey() it is constructed with 1 as well
        $this->object->expects($this->any())->method('getId')->will($this->returnValue(1));

        // So now I am calling on my API to generate a cache key based on
        //  values used here, and when I constructed my "known good" key
        $key = Hobis_Api_Cache_Key_Package::factory(
            array(
                'dynamicSuffixes'   => array($this->getRandomNumber(), $this->getObject()->getId()),
                'expiry'            => self::TEST_EXPIRY,
                'staticPrefix'      => self::TEST_KEY_PREFIX,
                'value'             => self::TEST_VALUE
            )
        );

        // Calling set via api
        $setStatus = Hobis_Api_Cache_Package::set($key);

        // Check that call was what we expect
        $this->assertTrue($setStatus);

        // Now let's retrieve the cached value so we can test if it's available
        $cachedValue = Hobis_Api_Cache_Package::get($key);

        // Test the attributes against "known good" values
        $this->assertSame($key->getKey(), $this->getKnownGoodKey());
        $this->assertSame($cachedValue, self::TEST_VALUE);
    }
    //-----
}

关于php - 我应该如何测试这个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17098163/

相关文章:

PHP:将动态数量的值添加到 MySQL 数据库中

javascript - jQuery $.getJSON 并遍历数组正在倾倒所有

ruby-on-rails - FactoryGirl - 无法写入未知属性和未定义方法 `name'

visual-studio - visual studio web 测试错误处理

php - 调用返回后,PHP 是否垃圾收集函数范围的对象?

数组中的 PHP 最短/最长字符串长度

php - 如何从字符串中删除重复的子字符串

javascript - 如何测试 react Hook 方法?

php - Symfony 在我的单元测试中模拟一个特定的方法

laravel - 使用 laravel 8 测试上传文件,assertExists 不起作用