Laravel actsAs返回空的用户属性

时间:2016-04-15 15:37:38

标签: php testing laravel-5 phpunit

问题

在测试环境中,访问auth中间件后面的视图具有空用户属性。如何使用其完整属性模拟经过身份验证的用户?

actAs的工作原理是因为跳过它会将我返回到登录页面,但是一旦我在个人资料页面上,就没有用户属性。

测试代码

$user = factory(App\Models\User::class, 'regular')->make() ;

$this->actingAs($user)
         ->visit('/profile') ;
         ->see('Welcome ' . $user->profile()['first_name']) ;

我尝试的事情

  • 传递会话变量(无效)

    $this->withSession([ 'user' => $user, 'profile' => $profile, 'address'=> $address ])
    
  • 从DB(工作)加载真实的现有用户

    $user = App\Models\User::where('uid', 72)->first() ;
    $this->actingAs($user) // works !
    
  • 持久存在ModelFactory对象(失败)

      $user = factory(App\Models\User::class, 'regular')->create() ;
    

错误

1) ProfileTest::testProfileViewRegularUser
A request to [https://url.com/profile] failed. Received status code [500].

/var/www/identity/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
/var/www/identity/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
/var/www/identity/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:61
/var/www/identity/tests/ProfileTest.php:42

Caused by
exception 'ErrorException' with message 'Trying to get property of non-object' in /var/www/identity/storage/framework/views/4882797a09bc7e305f1c9e6b7e749d48e58385ae.php:40
Stack trace:
#0 /var/www/identity/storage/framework/views/4882797a09bc7e305f1c9e6b7e749d48e58385ae.php(40): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', '/var/www/identi...', 40, Array)
#1 /var/www/identity/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(42): include('/var/www/identi...')
#2 /var/www/identity/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/var/www/identi...', Array)
#3 /var/www/identity/vendor/laravel/framework/src/Illuminate/View/View.php(147): Illuminate\View\Engines\CompilerEngine->get('/var/www/identi...', Array)
#4 /var/www/identity/vendor/laravel/framework/src/Illuminate/View/View.php(118): Illuminate\View\View->getContents()
#5 /var/www/identity/vendor/laravel/framework/src/Illuminate/View/View.php(83): Illuminate\View\View->renderContents()
#6 /var/www/identity/vendor/laravel/framework/src/Illuminate/Http/Response.php(53): Illuminate\View\View->render()
#7 /var/www/identity/vendor/symfony/http-foundation/Response.php(199): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#8 /var/www/identity/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1087): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#9 /var/www/identity/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(95): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View))

持久化模型

时出错
There was 1 error:

1) ProfileTest::testProfileViewRegularUser
ErrorException: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

/var/www/identity/vendor/laravel/framework/src/Illuminate/Support/helpers.php:740
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/QueryException.php:56
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/QueryException.php:39
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Connection.php:675
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Connection.php:629
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Connection.php:409
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Connection.php:365
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1963
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1337
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1621
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1621
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1590
/var/www/identity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1481
/var/www/identity/tests/ProfileTest.php:31

1 个答案:

答案 0 :(得分:1)

在请求授权上使用$this->user()->can()时遇到了类似的问题,如果我将用户传递到该路由的userResolver中,则对我有用

$user = factory(User::class)->create();
$this->actingAs($user)->visit('/profile')->setUserResolver(function () use ($user){
    return $user;
});