根据Jeffery way的书"Jeffrey Way Laravel Testing Decoded",我刚刚开始使用php mock,但我在第一次mock时遇到了问题。我一直在找它,似乎找不到问题所在。
<?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);
}
}发布于 2016-02-05 16:20:42
您应该对此进行更改:
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的期望将会失败,因为输入参数不匹配。
https://stackoverflow.com/questions/35207783
复制相似问题