脚本宝典收集整理的这篇文章主要介绍了phpunit – 用find()模拟Eloquent模型,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
__construct(Post $model){$this->model=$model}
$post = $this->model->find($id);
这里是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)); } }
Fatal error: Using $this when not in object context in ...\www\l4\vendor\mockery\mockery\library\Mockery\Generator.PHP(130) : eval()'d code on line 73
这显然是因为find()被声明为静态函数.现在,代码可以正常运行,因此如何在不失败的情况下成功模拟Eloquent模型.由于我们依赖于依赖注入,我必须非静态地调用find(),否则我可以只做Post :: find().
我想出的一个解决方案是在BaSEModel中创建一个非静态的find()替换
public function nsFind($id,$columns = array('*')) { return self::find($id,$columns); }
我做错了什么或你有更好的想法吗?
以上是脚本宝典为你收集整理的phpunit – 用find()模拟Eloquent模型全部内容,希望文章能够帮你解决phpunit – 用find()模拟Eloquent模型所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。