Php Laravel actingAs返回空用户属性 问题

Php Laravel actingAs返回空用户属性 问题,php,testing,laravel-5,phpunit,Php,Testing,Laravel 5,Phpunit,在测试上下文中,访问auth中间件后面的视图的用户属性为空。如何使用完整属性模拟经过身份验证的用户 actingAs之所以有效,是因为跳过它会使我返回到登录页面,但一旦我进入配置文件页面,就不会有任何用户属性 测试代码 我尝试过的事情 传递会话变量(无效) 从DB(works)加载真实的现有用户 持久化ModelFactory对象(失败) 错误 持久化模型时出错 在请求授权时使用$this->user()->can()时,我也遇到了类似的问题,如果我将用户传递到路由的userResol

在测试上下文中,访问
auth
中间件后面的视图的用户属性为空。如何使用完整属性模拟经过身份验证的用户

actingAs之所以有效,是因为跳过它会使我返回到登录页面,但一旦我进入配置文件页面,就不会有任何用户属性

测试代码 我尝试过的事情
  • 传递会话变量(无效)

  • 从DB(works)加载真实的现有用户

  • 持久化ModelFactory对象(失败)

错误 持久化模型时出错
在请求授权时使用
$this->user()->can()
时,我也遇到了类似的问题,如果我将用户传递到路由的userResolver中,它对我很有效

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

您解决过这个问题吗?@designvoid我没有使用工厂创建用户。我制作了自己的助手,将测试用户直接保存在数据库中,然后在每次测试之前加载它们,并继续如上所述(
$this->actingAs($user)
$this->withSession([ 'user' => $user, 'profile' => $profile, 'address'=> $address ])
$user = App\Models\User::where('uid', 72)->first() ;
$this->actingAs($user) // works !
  $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
$user = factory(User::class)->create();
$this->actingAs($user)->visit('/profile')->setUserResolver(function () use ($user){
    return $user;
});