单元测试Laravel的封口

时间:2014-03-31 19:48:06

标签: php unit-testing laravel mockery

以下代码中的闭包使得此代码非常难以测试。如何继续急于加载这些项目并保持完全可测试性?

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

我已经看到使用Mockery使用Mockery::on(),但我不认为这对阵列有用。

1 个答案:

答案 0 :(得分:2)

如果你嘲笑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!");