Laravel phpunit测试失败了.去意想不到的页面.只在测试中发生

jas*_*ton 11 php phpunit unit-testing laravel laravel-5

所以,我有一个页面上有一个带有"Create"值的按钮.当我单击"创建"按钮而不填写任何字段时,它会验证表单并在同一页面上显示错误消息.当我在浏览器中这样做时,它工作正常,但当我这样做时phpunit,它有意想不到的结果,我不知道为什么.

这是我的集成测试:

public function testCreateValidation()                                     
{ 
    $this->visit(route('patients.indexes.create', $this->patient->id));    
    $this->press('Create');                                                
    $this->seePageIs(route('patients.indexes.create', $this->patient->id));              
}   
Run Code Online (Sandbox Code Playgroud)

这就是结果:

There was 1 failure:
1) Tests\Integration\IndexControllerTest::testCreateValidation
Did not land on expected page [http://localhost/patients/69/indexes/create].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/patients/69/indexes/create'
+'http://localhost/patients'

/vagrant/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:141
/vagrant/tests/Integration/IndexControllerTest.php:51
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它被重定向到patients页面.

以下是create正在测试的Laravel 方法:

public function create($id)                                                         
{                                                                                   
    $index = $this->indexes->newInstance();                                         
    $patient = $this->patients->findOrFail($id);                                    

    return view('patient.index.create', ['index' => $index, 'patient' => $patient]);
} 
Run Code Online (Sandbox Code Playgroud)

以下是该create视图的相关部分:

<?= Form::open(['route' => array('patients.indexes.store', $patient->id), 'class' => 'form-horizontal']) ?>
    @include('patient.index._form')
    <?= Form::submit('Create', ['class' => 'btn btn-primary']) ?>
<?= Form::close() ?> 
Run Code Online (Sandbox Code Playgroud)

最后store发送给它的方法:

public function store(IndexRequest $request, $id)       
{                                                       
    $index = $this->indexes->newInstance();             
    $index->fill($request->all());                      
    $index->patient_id = $id;                           
    $index->save();                                     

    $patient = $index->patient;                         

    return redirect()->route('patients.edit', $patient);
} 
Run Code Online (Sandbox Code Playgroud)

我也使用FormRequest来验证输入:

public function rules()                   
{                                         
    return [                              
        'index_title' => 'required',      
        'index_description' => 'required',
    ];                                    
}  
Run Code Online (Sandbox Code Playgroud)

基本上,由于它在验证失败IndexRequest,IndexRequest应该将其踢回patients.indexes.create视图并显示错误.但由于某种原因,它被踢到患者页面(这只发生在测试中,如果我通过手动点击浏览器中的"创建"按钮进行尝试,它会按预期工作)

我以前遇到过这个问题,但从来没有能够解决它.有任何想法吗?

Bro*_*ary 4

听起来您遇到了CSRF问题。当您导航到浏览器中的表单时,Laravel 会存储一个特殊的令牌在浏览器的 cookie 和请求标头中然后,当您提交表单时,它会查找该令牌以确保您是提交表单的人。

当您使用 PHPUnit 进行测试时,不会发送该令牌,因此您要么必须自己发送它,要么必须从检查令牌的中间件中排除您的测试。排除测试是更简单的方法。

handle()在 Laravel 5.0 中,我通过重写中间件中的方法来做到这一点VerifyCsrfToken

class VerifyCsrfToken extends BaseVerifier {
    // Override the handle() method in the BaseVerifier class
    public function handle($request, Closure $next) {
        // When the App environment is 'testing', skip CSRF validation.
        if (app()->environment() === 'testing') {
            return $next($request);
        }

        return parent::handle($request, $next);
    }
}
Run Code Online (Sandbox Code Playgroud)

phpunit.xml默认情况下,PHPUnit 从Laravel 站点根目录中的文件中提取环境变量。该APP_ENV变量控制应用程序环境名称,默认为“testing”。如果您的不同,则需要更改我提供的代码以匹配它。