调用Laravel 5+Codeception函数测试中未定义的方法Session::has()


Call to undefined method Session::has() in Laravel 5 + Codeception functional testing

我用Codeception在Laravel 5中运行了一些功能测试。

以下是的样本测试

<?php
use Illuminate'Foundation'Testing'WithoutMiddleware;
use Illuminate'Foundation'Testing'DatabaseMigrations;
use Illuminate'Foundation'Testing'DatabaseTransactions;
class HomePageTestCest
{
    use WithoutMiddleware;
    public function _before(FunctionalTester $I)
    {
        //some setup to run
    }
    public function _after(FunctionalTester $I)
    {
    }
    // tests
    public function tryToTest(FunctionalTester $I)
    {
        $I->wantToTest('If i can go to home page without errors');
        $I->amOnPage('/');
        $I->canSee('WELCOME TO HOMEPAGE');
    }
}

但我在运行测试时出现了以下错误:

Fatal error: Call to undefined method Session::has() in /var/www/laravel5/storage/framework/views/e7953b3ce9b90e51d4cfdb279790953bbe25dc9a.php on line 225

在第225行,我有:

<?php if(Session::has('someKey')): ?>
  //some html code
<?php endif; ?>

我认为问题出在会话驱动程序上。我也尝试过实现这个,但没有任何改变。

希望能得到一些帮助。提前谢谢。

LARAVEL 5.2中,他们还创建了类似助手的会话类

在哪里你可以像这个一样使用它

session()->has('key')

参见文档会议

namespace App'Http'Controllers;
// import session class using alias
use Session;
class SampleController exntends Controller {
    public function __construct() {
    }
    public function index() {
        // print session values
        var_dump(Session::all());
    }
}

感谢hamedmehryar提供的解决方案。我不知道为什么,但出于某种原因,我必须为facade定义整个命名空间,即使它们已经在config/app.php中的"别名"中定义了。例如:

Session::has()

应该像一样

Illuminate'Support'Facades'Session::has()

我在这里解释过,所以如果有人面临这个问题,就可以从这里解决它。

不要忘记

CCD_ 1。

并使用类似的会话

$request->session()->has('key');