普通网友 2015-04-21 19:27
浏览 11
已采纳

phpspec force方法返回值

I am trying to test a class method that gets its data from another method.
So I did this :

function it_should_return_json_file_as_array()
{
    $this->exist()->willReturn(true);
    $this->read()->willReturn("{\"key\":\"value\"}");
    $this->getContent()->shouldHaveKeyWithValue('key', 'value');
}

But when I launched phpspec run, I got this:

[InvalidArgumentException]
String "" is not a valid classname. 
Please see reference document: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md  

What is going on?

  • 写回答

1条回答 默认 最新

  • dtzh131555 2015-04-22 11:07
    关注

    It is not possible to stub methods of a class you are specifying, and it is not possible on purpose.

    PhpSpec is trying to tell you there's a problem with the way you decided to design.

    See My top ten favourite PhpSpec limitations - Limitation #2:

    This limitation can become very visible in the case of using inheritance to extend behaviour. We inherit a class and want to add a new method that internally delegates some behaviour to a parent class method. We cannot mock or stub that method. This has lead me again and again to favour composition over inheritance, which is a golden principle in OO design. In the few cases in which inheritance is justified, we can isolate the reusable behaviour into a separate object and use composition in the parent object, allowing us to replace the collaborator with a double.

    Also read Partial Mocking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测