【问题标题】:Test if a user has access to a link in laravel 5.1测试用户是否可以访问 laravel 5.1 中的链接
【发布时间】:2015-11-12 00:53:53
【问题描述】:

我是 phpUnit 和 Laravel 5.1 的新手

我要测试权限:

我想确保应该为管理员显示的链接(管理)不应该被简单的用户看到...

要登录,我可以这样做:

protected function login_standard_user()
{
    return $this->visit('login')
        ->type('user@user.com', 'email')
        ->type('user', 'password')
        ->press('Login');
}

但是我怎么能测试没有链接“管理”而不使 phpunit 失败???

【问题讨论】:

    标签: php phpunit laravel-5.1


    【解决方案1】:

    我不知道这是否是正确的做法,但你可以在你的代码周围放置一个try catch

    $this->visit('login')
    ->type('user@user.com', 'email')
    ->type('user', 'password')
    ->press('Login');
    
    try{
        $this->click('Administration')
        ->seePageIs('/admin');
    } catch(Exception $e){
        echo "Exception Catched";
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      protected function login_standard_user()
      {
          return $this->visit('login')
              ->type('user@user.com', 'email')
              ->type('user', 'password')
              ->press('Login')
              ->dontSee('Administration')
              ;
      }
      

      我还没有验证它是否有效,但这里记录了它,

      http://laravel.com/docs/5.1/testing#application-testing

      ...所以应该没问题。

      【讨论】:

        猜你喜欢
        • 2016-01-25
        • 2020-03-16
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多