我上的课主要是关于这个的
$newItem = new SeriesDiscountDecorator($item);
$cart->remove($item);
$cart->add($newItem);我的规格看起来像是
$decoratedItem1->beConstructedWith([$item1]);
$cart->add($decoratedItem1)->shouldBeCalled();但是PHPSpec说由于参数错误,没有进行正确的$cart->add调用。事实上,我的$decoratedItem与Cart类创建的对象不同。如何编写这样的规范,对吗?
PHPSpec返回:
method call:
- add(Domain\Discount\Decorator\SeriesDiscountDecorator:000000006b0917e60000000063743658 Object (
'decoratedItem' => Double\ItemInterface\P66:000000006b0917a60000000063743658 Object (
'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
)
))
on Double\Domain\Cart\Cart\P65 was not expected, expected calls were:
- add(exact(Double\Domain\Discount\Decorator\SeriesDiscountDecorator\P69:000000006b0917f90000000063743658 Object (
'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
'decoratedItem' => PhpSpec\Wrapper\Collaborator:000000006b0917b30000000063743658 Object (
'prophecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
)
)))发布于 2016-05-24 15:16:23
$cart->add(SeriesDiscountDecorator::class)->shouldBeCalled();这似乎是测试它的唯一方法,只要您在指定的类中实例化对象,并且不返回它。
发布于 2022-02-02 19:36:52
这是一个令人难以置信的老版本,但是使用phpspec 2.5.8。我能够在collaborator上使用getWrappedObject()方法完成这个任务。因此,该规范将如下所示:
$decoratedItem1->beConstructedWith([$item1]);
$cart->add($decoratedItem1->getWrappedObject())->shouldBeCalled();https://stackoverflow.com/questions/34665930
复制相似问题