2016-04-13 33 views
0

我在Codera上運行Laravel 5中的一些功能測試。調用未定義的方法Session :: has()在Laravel 5 + Codeception功能測試中

下面是一個樣本測試

<?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; ?> 

我想問題在於會話驅動程序。我也嘗試實施this,但沒有任何改變。

希望能得到一些幫助。提前致謝。

+1

寫完整的命名空間爲:'Illuminate \ Support \ Facades \ Session :: has('something')' – hamedmehryar

+0

是否需要專門爲功能測試編寫完整的命名空間? –

+0

@hamedmehryar你的解決方案工作,但你能解釋爲什麼我必須定義整個命名空間? –

回答

1

LARAVEL 5.2他們還創造像幫手

會話類,其中你可以使用它像這樣

session()->has('key') 

DOCUMENTATION SESSION

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()); 
    } 

} 
+0

嘿,對不起,我完全不知怎的看着你的解決方案。你的解決方案也可以。但是你知道原因嗎,爲什麼Session :: not working? thnx提前。 –

+0

我相信別名類是自動需要的,不像laravel 4.這是一個鏈接,討論你的問題。如果要使用Facades別名,請使用Use Session導入類;那麼你已經可以使用Session :: has('key')。我更新我的答案,所以你可以按照我的意思 –

+0

嘿thnx回覆。是的,我已經嘗試過,但沒有工作。唯一可用於測試的是當我使用像Illuminate \ Support \ Facades \ Session這樣真正有線的完整路徑時,就像我在下面的答案中所述。我正在尋找一個理由或最準確的答案。 –

0

不要忘記

use Illuminate\Http\Request;

,並使用會話這樣

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

1

由於hamedmehryar的解決方案。我不知道爲什麼,但由於某些原因,我必須爲外觀定義完整的命名空間,即使它們已經在config/app.php中的「別名」中定義。例如:

Session::has() 

應該像

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

我解釋在這裏,如果任何人面對這個問題,就能從這裏解決。

相關問題