Laravel actsAs返回空的用户属性

Problem

In test context, accessing a view behind auth middleware has empty user attributes. How do I simulate an authenticated user with their full attributes ?

The actingAs works because skipping it returns me to the login page, but once I'm on the profile page non of the user attributes are there.

Test code

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

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

Things I've tried

  • Passing session variables (has no effect)

    $this->withSession([ 'user' => $user, 'profile' => $profile, 'address'=> $address ])
    
  • Loading a real existing user from DB (works)

    $user = App\Models\User::where('uid', 72)->first() ;
    $this->actingAs($user) // works !
    
  • Persisting the ModelFactory object (fails)

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

Error

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))

Error when persisting model

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