测试递归方法 [英] Test a recursive method

查看:80
本文介绍了测试递归方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个方法

public function get($key)
{
    if (!($time = $this->driver->get($key))) {
        if ($key == self::LAST_UPDATE_KEY) {
            $time = new \DateTime();
            $this->driver->set($key, $time);
        } else {
            $time = $this->get(self::LAST_UPDATE_KEY); // need test this condition
        }
    }

    return $time;
}

来自驱动程序的第一个请求数据应该返回null,而第二个含义对我来说是必要的.

The first request data from the driver should return null, while the second meaning is necessary to me.

我写了一个测试

public function testGetEmpty()
{
    $time = new \DateTime();
    $driver_mock = $this
        ->getMockBuilder('MyDriver')
        ->getMock();
    $driver_mock
        ->expects($this->once())
        ->method('get')
        ->with('foo')
        ->will($this->returnValue(null));
    $driver_mock
        ->expects($this->once())
        ->method('get')
        ->with(Keeper::LAST_UPDATE_KEY)
        ->will($this->returnValue($time));

    $obj = new Keeper($driver_mock);
    $this->assertEquals($time, $obj->get('foo'));
}

执行时返回错误

Expectation failed for method name is equal to <string:get> when invoked 1 time(s)
Parameter 0 for invocation MyDriver::get('foo') does not match expected value.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'last-update'
+'foo'

很久没有写单元测试了,很多都忘记了.帮我理解.

Long time i did not write the unit tests, and many forgot. Help me understand.

推荐答案

需要使用 $this->at(0)$this->at(1)代码>

Need use $this->at(0) and $this->at(1)

这篇关于测试递归方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆