我试图用Mockery模拟Eloquent模型.模型正在Controller中注入
__construct(Post $model){$this->model=$model}
Run Code Online (Sandbox Code Playgroud)
现在我find()在控制器中调用该函数
$post = $this->model->find($id);
Run Code Online (Sandbox Code Playgroud)
这里是PostsController的测试
class PostsTest extends TestCase{
protected $mock;
public function setUp() {
parent::setUp();
$this->mock = Mockery::mock('Eloquent', 'Posts'); /*According to Jeffrey Way you have to add Eloquent in this function call to be able to use certain methods from model*/
$this->app->instance('Posts', $this->mock);
}
public function tearDown() {
Mockery::close();
}
public function testGetEdit()
{
$this->mock->shouldReceive('find')->with(1)->once()->andReturn(array('id'=>1));
$this->call('GET', 'admin/posts/edit/1');
$this->assertViewHas('post', array('id'=>1));
}
}
Run Code Online (Sandbox Code Playgroud)
运行PhpUnit给我错误:
Fatal error: Using $this when not in object context in ...\www\l4\vendor\mockery\mockery\library\Mockery\Generator.php(130) : …Run Code Online (Sandbox Code Playgroud)