doufang2023 2013-02-12 17:44
浏览 60
已采纳

PHPUnit存根仅适用于第一种方法

I'm trying to stub Amazon's instance metadata class because it calls an internal URL that only works within an EC2 instance and can't be called from outside. My problem is that the method "send" is not recognized. The method "get" works fine though. This is the error Fatal error: Call to undefined method Stub\Amazon\StubInstanceMetadata::send() in ...etc, etc

    $stub = $this->getMockBuilder('Aws\Common\InstanceMetadata\InstanceMetadataClient')
                 ->disableOriginalConstructor()
                 ->getMock();

    $stub->expects($this->any())
         ->method('get')
         ->will($this->returnValue($this));

    $stub->expects($this->any())
         ->method('send')
         ->will($this->returnValue(json_encode(array('test' => 'value'))));

EDIT: This ended up being a combination of two different issues. The first one being the one pointed out by @fab. The second being that to return a reference to $this, PHPUnit has it's very own method, so I should have done this:

    $stub->expects($this->any())
         ->method('get')
         ->will($this->returnSelf()); // don't use returnValue() here
  • 写回答

1条回答 默认 最新

  • duanji7182 2013-02-12 18:48
    关注

    I don't know the original class but it looks like send is not actually a method of it but rather called via magic __call. So if you want to mock it, you will have to do it explicitly:

    ...->getMock(array('get', 'send'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效