0

我对 phpspec 还很陌生,但通常我会在遇到问题时找到解决方案,但这很难。

我尝试了许多不同的方法,但没有找到解决方案。我正在使用 Symfony2。

我有一个要测试的课程:

class MyClass
{

    public function getDataForChildren(MyObject $object)
    {
        foreach ($object->getChildren() as $child) {
            $query = \json_decode($child->getJsonQuery(), true);
            $data = $this->someFetcher->getData($query);
            $child->setData($data);
        }
        return $object;
    }

}

这是我的规范类的外观:

class MyClassSpec
{

    function let(SomeFetcher $someFetcher)
    {
        $this->beConstructedWith($someFetcher);
    }

    function it_is_initializable()
    {
        $this->shouldHaveType('MyClass');
    }

    function it_should_get_data_for_children_and_return_object(
        MyClass $object,
        MyClass $child, // it means that MyClass has a self-reference to MyClass
        $someFetcher
    )
    {
        $query = '{"id":1}';

        $returnCollection = new ArrayCollection(array($child));

        $object->getChildren()->shouldBeCalled()->willReturn($returnCollection);

        $child->getJsonQuery()->shouldBeCalled()->willReturn($query);

        $someFetcher->getData($query)->shouldBeCalled();

        $this->getDataForChildren($object);
    }

}

运行 phpspec 后,我收到此错误:

warning: json_decode() expects parameter 1 to be string, object given in

我不知道如何解决这个问题。如果有人有线索,请帮助。

4

1 回答 1

5

这是 PhpSpec 的一个常见绊脚石,声明:

   MyClass $child

意味着 $child 的 Collaborator 对象将设置为与 MyClass 相同的接口。当在 SUT(您正在测试的类)中调用 child->getJsonQuery() 时,它将返回MethodProphecy而不是您期望它返回的字符串。

您想说的是,您的 ArrayCollection 将包含$child 本身(它是一个 Collaborator 对象),而是包含协作者所环绕的真实对象。你这样做:

$returnCollection = new ArrayCollection(array($child->getWrappedObject()));

此外,您不应该在同一个协作者上同时使用(即是多余的)shouldBeCalled() 和 willReturn(),一个或另一个就足够了。如果您已经指定了协作者将返回的内容,那么很明显它将在 SUT 中被调用。shouldBeCalled() 应该在测试的“断言”部分使用,以确认 Collaborator 是用预期的参数调用的,或者是在正确的时间调用的。

您的最终 SUT 和规范应如下所示:

   class MyClass
   {

        /**
         * @var SomeFetcher
         */
        private $someFetcher;

        public function getDataForChildren(MyObject $object)
        {
            foreach ($object->getChildren() as $child) {
                $query = \json_decode($child->getJsonQuery(), true);
                $data = $this->someFetcher->getData($query);
                $child->setData($data);
            }
            return $object;
        }

        public function getJsonQuery()
        {
        }

        public function setData()
        {
        }

        public function __construct(SomeFetcher $someFetcher)
        {
            $this->someFetcher = $someFetcher;
        }
    }

class MyClassSpec extends ObjectBehavior
{

    function let(SomeFetcher $someFetcher)
    {
        $this->beConstructedWith($someFetcher);
    }

    function it_should_get_data_for_children_and_return_object(
        MyObject $object,
        MyClass $child, // it means that MyClass has a self-reference to MyClass
        SomeFetcher $someFetcher
    )
    {
        $query = '{"id":1}';

        $returnCollection = new ArrayCollection(array($child->getWrappedObject()));

        $object->getChildren()->willReturn($returnCollection);

        $child->getJsonQuery()->willReturn($query);
        $child->setData(Argument::any())->shouldBeCalled();

        $someFetcher->getData(array('id' => 1))->shouldBeCalled();

        $this->getDataForChildren($object);
    }

}

另外,线

$query = \json_decode($child->getJsonQuery(), true);

将在 $query 中生成一个关联数组,即 array('id' => 1) (这是 json_encode 的第二个 'true' 参数规定的),因此您希望 $someFetcher->getData() 被调用后者,因此:

$someFetcher->getData(array('id' => 1))->shouldBeCalled();
于 2015-01-01T14:58:48.560 回答