2017-01-04 236 views
0

我試圖做第一個behat測試,但是當我運行它時,我得到了TokenMis ...錯誤。我知道爲什麼。因爲它沒有渲染_token到我的形式。我檢查有:Laravel behat CSRF令牌

$這個 - >的getSession() - > GETPAGE() - > getHtml()

和我:

<input type="hidden" name="_token" value="">

Normaly它必須是:

<input type="hidden" name="_token" value="Atf3lzQ7McA4zQorUu089X3zPsrLVRE8TvQkVOtm">

所以我的問題是我做錯了什麼?

我的功能看起:

Feature: User Login 
To test if user can login to system 

Scenario: Successful Authentication 
    Given I sign in "email" "pw" 
    Then I should be sign in 

配置:

default: 
    autoload: 
    '': %paths.base%/features/bootstrap 
    suites: 
    default: 
     contexts: 
     - 'FeatureContext' 
     paths: 
     - %paths.base%/features/default.feature 
    user_login_module: 
     contexts: 
     - 'UserLoginContext' 
     paths: 
     - %paths.base%/features/user-login.feature 
    extensions: 
    Laracasts\Behat: 
     env_path: .env.laravel.behat 
    Behat\MinkExtension: 
     default_session: laravel 
     base_url: http://localhost:8080 
     laravel: ~ 

FeatureContext文件(UserLoginContext):

<?php 

use Behat\Behat\Context\Context; 
use Behat\Gherkin\Node\PyStringNode; 
use Behat\Gherkin\Node\TableNode; 
use Behat\MinkExtension\Context\MinkContext; 
use Laracasts\Behat\Context\Migrator; 
use Laracasts\Behat\Context\DatabaseTransactions; 
use PHPUnit_Framework_Assert as PHPUnit; 

class UserLoginContext extends MinkContext implements Context 
{ 
    use Migrator, DatabaseTransactions; 

    /** 
    * Initializes context. 
    * 
    * Every scenario gets its own context instance. 
    * You can also pass arbitrary arguments to the 
    * context constructor through behat.yml. 
    */ 
    public function __construct() 
    { 
    } 

    /** 
    * Loads Laravel classes 
    * 
    * @static 
    * @beforeSuite 
    * @return mixed 
    */ 
    public static function bootstrapLaravel() 
    { 
     $app = require __DIR__.'/../../bootstrap/app.php'; 

     $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); 

     return $app; 
    } 

    /** 
    * @Given I sign in :email :password 
    */ 
    public function iSignIn($email, $password) 
    { 
     $this->visit('admin-panel'); 

     $this->fillField('email', $email); 
     $this->fillField('password', $password); 
     $this->pressButton('Prisijungti'); 
    } 

    /** 
    * @Then I should be sign in 
    */ 
    public function iShouldBeSignIn() 
    { 
     PHPUnit::assertTrue(Auth::guard('admin')->check()); 

     //$this->assertPageAddress('admin-panel/home'); 
    } 

} 

一樣,用錯誤的變量。拋出錯誤未定義錯誤變量

回答

0

請在刀片文件中包含此CSRF helper function。這將自動生成以下代碼<input type="hidden" name="_token" value="2WXMw1BMY2o5irBKcXyzJBDeai9colsqFuVpNGXm">

<form action = "{{ url('test') }}" method="POST"> 
{{ csrf_field() }} 
.... 
.... 
</form> 

編輯1:

我覺得這是一個issue與貝哈特laravel擴展本身,而不是你的代碼。

+0

嗯..當我使用硒,它打開瀏覽器的作品。現在我想如何讓它在沒有硒的情況下工作。 – Ernestyno