本文介绍了Mocking Laravel雄辩模特儿 - 如何用Mockery设置公共财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的PHPUnit测试中使用一个模拟对象(Mockery)。模拟对象需要有一些公共方法和一些公共属性。该课是Laravel雄辩模特儿。我试过这个: $ mock = Mockery :: mock('User');
$ mock-> shouldReceive('hasRole') - > once() - > andReturn(true); //工作正常
$ mock-> roles = 2; //怎么做?当前返回错误
$ this-> assertTrue(someTest($ mock));
...但设置公共属性返回此错误:
当嘲笑一个简单的类时,不会返回此错误,但是当我尝试模拟一个有说服力的模型时返回。我做错了什么?
解决方案
如果你想获得这个属性的这个值,只要使用它:
$ mock - > shouldReceive('getAttribute')
- > with('role')
- > andReturn(2);
如果您调用 $ user-> role
你会得到 - 2
( $ user
- 其 code> mock class)
I want to use a mock object (Mockery) in my PHPUnit test. The mock object needs to have both some public methods and some public properties set. The class is a Laravel Eloquent model. I tried this:
$mock = Mockery::mock('User');
$mock->shouldReceive('hasRole')->once()->andReturn(true); //works fine
$mock->roles = 2; //how to do this? currently returns an error
$this->assertTrue(someTest($mock));
... but setting the public property returns this error:
This error is not returned when mocking a simple class, but is returned when I try to mock an Eloquent model. What am I doing wrong?
解决方案
If you want getting this property with this value, just use it:
$mock ->shouldReceive('getAttribute')
->with('role')
->andReturn(2);
If you call $user->role
you will get - 2
($user
- its User
mock class)
这篇关于Mocking Laravel雄辩模特儿 - 如何用Mockery设置公共财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!