gpt4 book ai didi

phpspec - 方法返回对象而不是字符串

转载 作者:行者123 更新时间:2023-11-28 21:01:35 25 4
gpt4 key购买 nike

我对 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

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

最佳答案

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

   MyClass $child

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

您想说的是,您的 ArrayCollection 将包含不是 $child 本身(这是一个 Collaborator 对象),而是包含 collaborator 的真实对象。你这样做:

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

In addition, you should not be using (i.e. is is superfluous) both shouldBeCalled() and willReturn() on the same Collaborator, one or the other is sufficient. If you've specified what the collabrator will return, it is clear that it is going to be called witin the SUT. shouldBeCalled() should be used in the "assert" part of the test in order to confirm that the Collaborator was called with the expected arguments, or at the right time.

您的最终 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();

关于phpspec - 方法返回对象而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com