3

下面代码中的闭包使得这段代码很难测试。我怎样才能继续急切地加载这些项目并保持完整的可测试性?

public function scopeWithCompanyPreferences(Builder $builder)
{
    return $builder->with([
            'companies' => function ($query) {
                $query->with('companies');
                $query->with('preference_settings');
                $query->with('parent_company');
            }
        ]);
}

我已经看到使用 Mockery 使用 of Mockery::on(),但考虑到数组,我认为这没有用。

4

1 回答 1

4

如果您要模拟该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!");
于 2014-03-31T21:11:33.643 回答