测试共享给所有视图的数据以确保其存在

时间:2019-02-19 14:31:46

标签: laravel laravel-5 phpunit laravel-5.7

我在AppServiceProvider的laravel项目中与所有视图共享一些东西:

public function boot()
    {

        $userLocation = geoip()->getLocation(geoip()->getClientIP());

        view()->share(['user' => \Auth::user(), 'userLocation' => $userLocation]);
    }

但是当我对此进行测试时,它们没有出现:

/** @test */
public function page_has_recent_reviews()
{
    $response = $this->get(route('home'));

    $response->assertSuccessful();

    $response->assertViewHasAll(['recentReviews', 'recentPhotos']);

    $data = $response->getOriginalContent()->getData();

    print_r(array_keys($data)); exit;

}

转储的内容包含:

Array
(
    [0] => recentReviews
    [1] => recentPhotos
)

但是没有数据共享给视图。一个如何测试呢?

1 个答案:

答案 0 :(得分:0)

因此,一段时间后,我发现如果您在AppServiceProvider(引导方法)的composer callback中共享数据,则它将在测试中呈现:

view()->composer('*', function($view) use ($userLocation, $ratingStars, $icons)
    {
        $view->with(['userLocation' => $userLocation, 'user' => \Auth::user());
    });