dongnachuang6635
2014-08-28 07:58
浏览 74
已采纳

Phpunit mock没有效果

code:

class MockMe
{
    public function mockMeee()
    {
        return 'Im not mocked';
    }
}

test:

$sut = new MockMe();
$this
    ->getMock(get_class($sut))
    ->expects($this->any())
    ->method('mockMeee')
   ->will($this->returnValue('Im finally mocked'));
echo $sut->mockMeee();

this outputs the original "Im not mocked", but it supposed to send the Im finally mocked text. What is wrong?

EDIT: done:

$stub = $this->getMock('MockMe');
$stub->method('mockMeee')->willReturn('Im finally mocked');
echo $stub->mockMeee();

图片转代码服务由CSDN问答提供 功能建议

代码:

  class MockMe 
 {
 public  function mockMeee()
 {
 return'Im not mocked'; 
} 
} 
   
 
 

test: < pre> $ sut = new MockMe(); $ this - &gt; getMock(get_class($ sut)) - &gt; expect($ this-&gt; any()) - &gt ; method('mockMeee') - &gt; will($ this-&gt; returnValue('Im finally mocked')); echo $ sut-&gt; mockMeee();

这会输出原始“Im not mocked”,但它应该发送 Im finally mocked 文本。 有什么问题?

编辑:完成:

  $ stub = $ this-&gt; getMock('MockMe'); 
  $ stub-&gt;方法('mockMeee') - &gt; willReturn('我终于被嘲笑'); 
echo $ stub-&gt; mockMeee(); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwei1904 2014-08-28 08:04
    已采纳

    You are constructing the real MockMe, then building a mock that you are doing nothing with. I think your test should be something like:

    $sut = $this->getMock('MockMe');
    
    $sut->expects($this->any())
        ->method('mockMeee')
        ->will($this->returnValue('Im finally mocked'));
    
    echo $sut->mockMeee();
    

    Refer to http://phpunit.de/manual/4.2/en/test-doubles.html for more information on PHPUnit mocks.

    打赏 评论

相关推荐 更多相似问题