[英]Unit testing a closure in Laravel
The closure in the code below has made this code very difficult to test. 下面代码中的闭包使此代码很难测试。 How can I continue to eager load these items and maintain full testability?
我如何继续渴望加载这些项目并保持完整的可测试性?
public function scopeWithCompanyPreferences(Builder $builder)
{
return $builder->with([
'companies' => function ($query) {
$query->with('companies');
$query->with('preference_settings');
$query->with('parent_company');
}
]);
}
I've seen using Mockery the use of Mockery::on()
, but I don't think that's useful given the array. 我已经看到使用Mockery来使用
Mockery::on()
,但是鉴于数组,我认为这没有用。
If you're mocking the with
method, you should be able to use Mockery::on()
like this: 如果要模拟
with
方法,则应该可以使用Mockery::on()
这样:
$b = \Mockery::mock("your_builder_class");
$b->shouldReceive("with")
->with(\Mockery::on(function($x){
// test $x any way you like, for example...
// ...a simple check to see if $x["companies"] is a function
return is_callable($x["companies"]);
}))
->once()
->andReturn("hello!");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.