我在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; ?>我认为问题在于会话驱动程序。我也试图实现这,但是没有什么改变。
希望能得到一些帮助。提前谢谢。
发布于 2016-04-14 06:53:13
在LARAVEL5.2中,他们还创建了类似于helper的会话类
在那里你可以像这样利用它
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());
}
}发布于 2016-04-14 09:25:44
感谢hamedmehryar的解决方案。我不知道为什么,但出于某种原因,我必须为外观定义整个命名空间,即使它们已经在config/app.php内部的“别名”中定义。例如:
Session::has()应该就像
Illuminate\Support\Facades\Session::has()我在这里解释过,如果任何人面对这个问题,就能从这里解决它。
发布于 2016-04-14 07:04:39
别忘了
use Illuminate\Http\Request;。
然后像这样使用会话
$request->session()->has('key');
https://stackoverflow.com/questions/36599339
复制相似问题