【问题标题】:Laravel actingAs returns empty user attributesLaravel actuatorAs 返回空的用户属性
【发布时间】:2016-08-07 16:15:12
【问题描述】:

问题

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

actingAs 有效,因为跳过它会返回登录页面,但是一旦我进入个人资料页面,用户属性就不存在了。

测试代码

$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 ])
    
  • 从数据库加载一个真实的现有用户(工作)

    $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

【问题讨论】:

  • 你解决过这个问题吗?
  • @designvoid 我没有使用工厂来创建用户。我制作了自己的助手,将测试用户直接保存在数据库中,然后在每次测试之前加载它们并继续如上($this->actingAs($user)

标签: php testing laravel-5 phpunit


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-27
    • 2023-04-03
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多