0

我在模型构建器中发现了一些奇怪的行为,有人可以向我解释为什么会发生这种情况吗?

这是我的测试代码:

class PlaceTest extends \PHPUnit_Framework_TestCase
{
    const API_KEY = 'test-api';

    public function testConstruct()
    {
        $google = $this->getMockBuilder('GusDeCooL\GooglePhp\Google')
            ->setConstructorArgs(array(self::API_KEY))
                            ->setMethods(array('getKey'))
            ->getMock();
        $google->expects($this->any())
            ->method('getKey')
            ->will($this->returnValue(self::API_KEY));

        /* @var $google \GusDeCooL\GooglePhp\Google */
        $place = new Place($google);
        $this->assertInstanceOf('GusDeCooL\GooglePhp\Component\Place\Place', $place);
        $this->assertInstanceOf('GusDeCooL\GooglePhp\Google', $place->getParent());
        $this->assertEquals(self::API_KEY, $place->getKey());
        return $place;
    }



    /**
     * @param Place $place
     *
     * @depends testConstruct
     */
    public function testGetKey(Place $place)
    {
        $this->assertInstanceOf('GusDeCooL\GooglePhp\Google', $place->getParent());
        $this->assertEquals(self::API_KEY, $place->getKey());
    }
}

这是实际课程的代码

<?php



namespace GusDeCooL\GooglePhp\Component\Place;

use GusDeCooL\GooglePhp\Component\ChildInterface;
use GusDeCooL\GooglePhp\Google;
use GusDeCooL\GooglePhp\Place\Nearby;

class Place implements ChildInterface
{
    /**
     * @var Google
     */
    private $parent;

    /**
     * @var Nearby
     */
    private $nearby;

    public function __construct(Google $parent)
    {
        $this->setParent($parent);
    }


    /**
     * API Key
     * @return string
     */
    public function getKey()
    {
        return $this->getParent()->getKey();
    }
}

运行测试时,PlaceTest::testConstruct()执行测试$place->getKey()时通过了测试,但出现错误PlaceTest::testGetKey()

这是怎么回事?

4

1 回答 1

0

这似乎是模型构建器的限制。 http://phpunit.de/manual/3.7/en/test-doubles.html

我们不能将模型对象传递到另一个测试范围。

如果您发现其他原因,请发表评论。

在此处输入图像描述

于 2013-11-11T17:26:38.747 回答