2016-02-04 61 views
0

我刚开始使用php嘲笑继Jeffery方式书“Jeffrey Way Laravel Testing Decoded”,但我在第一次模拟时遇到了问题。我一直在看它似乎无法找到问题。PHP:错误嘲弄

<?php 
namespace BB8\Tests; 

use BB8\App\Generator; 
use BB8\App\File; 
use Mockery; 
class GeneratorTest extends \PHPUnit_Framework_TestCase 
{ 
    public function testMockery() 
    { 
     $mockedFile = Mockery::mock(File::class); 
     $mockedFile->shouldReceive('put') 
        ->with('foo.txt', 'foo bar bar') 
        ->once(); 
     $generator = new Generator($mockedFile); 
     $generator->fire(); 
    } 
} 

抛出的错误是

Mockery\Exception\NoMatchingExpectationException: No matching handler found 
for Mockery_0_BB8_App_File::put("foo.txt", "foo bar"). 
Either the method was unexpected or its arguments matched 
no expected argument list for this method 

我所有的方法来实现但它不能正常工作。

我需要帮助,似乎无法找出问题所在。

生成器类

namespace BB8\App; 

class Generator 
{ 
    protected $file; 

    public function __construct(File $file) 
    { 
    $this->file = $file; 
    } 

    protected function getContent() 
    { 
    return 'foo bar'; 
    } 

    public function fire() 
    { 
    $content = $this->getContent(); 
    $this->file->put('foo.txt', $content); 
    } 
} 
+0

你可以发布'Generator'类的代码吗? – Matteo

+0

@Matteo添加了Generator类 –

回答

0

你应该改变这样的:

public function testMockery() 
{ 
    $mockedFile = Mockery::mock(File::class); 
    $mockedFile->shouldReceive('put') 
       ->with('foo.txt', 'foo bar bar') 
       ->once(); 
    $generator = new Generator($mockedFile); 
    $generator->fire(); 
} 

这样:

public function testMockery() 
{ 
    $mockedFile = Mockery::mock(File::class); 
    $mockedFile->shouldReceive('put') 
       ->with('foo.txt', 'foo bar') 
       ->once(); 
    $generator = new Generator($mockedFile); 
    $generator->fire(); 
} 

的问题是,getContent()将返回'foo bar',不'foo bar bar',因此您的期望对于put w生病失败,因为输入参数不匹配。

+0

这是否解决了您的问题或者是否存在其他问题? –