我正在使用 Laravel 5.1,每当我测试一个包含两个表单的页面时,总是会提交第二个表单。如果我删除第二种形式,或交换形式的顺序,测试工作(但其他测试然后中断)。该页面在浏览器中的行为符合预期。感谢您的帮助。
edit.blade.php
@section('content')
<!-- Update Form -->
{!! Form::model($article,
[
'id'=>'editForm',
'method' => 'PATCH',
'action' => ['ArticlesController@update', $article->id]
]) !!}
<!-- Title Field -->
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['id' => 'title']) !!}
<!-- Content Field -->
{!! Form::label('content', 'Content:') !!}
{!! Form::textarea('content', null, ['id' => 'content']) !!}
<!-- Save Button -->
{!! Form::submit('Save', ['id' => 'save']) !!}
{!! Form::close() !!}
<!-- Delete Form -->
{!! Form::model($article,
[
'id'=>'deleteForm',
'method' => 'DELETE',
'action' => ['ArticlesController@destroy', $article->id]
]) !!}
<!-- Delete Button -->
{!! Form::submit('Delete', ['id' => 'delete']) !!}
{!! Form::close() !!}
@stop
ArticlesTest.php
/**
* @group articles
* @test
*/
public function it_edits_an_article()
{
//$this->markTestSkipped('Test doesn\'t work with two forms on one page.');
$article1 = factory(App\Articles\Article::class)->make();
$article2 = factory(App\Articles\Article::class)->make();
$user = factory(User::class)->create();
$user->articles()->save($article1);
$this->seeInDatabase('articles',
[
'title' => $article1->title,
'user_id' => $user->id
]);
$this->actingAs($user)
->visit($this->articlesUrl)
->see($article1->title)
->visit('/articles/'.$article1->slug.'/edit')
->type($article2->title, '#title')
->type($article2->content, '#content')
->press('Save') //this presses the delete button
->seePageIs($this->articlesUrl)
->see($article2->title)
->visit('/whats-new/'.$article1->slug)
->see($article2->content)
->visit('/latest/'.$article2->slug)
->see($article2->content)
->seeInDatabase('articles', ['title' => $article2->title]);
}
最佳答案
您的两个表单可能有一个带有文本“保存”的按钮
所以当你使用
->press('Save')
它们都被点击了。
关于php - 在一页上测试两个表单 `press` 指令总是提交第二个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34550588/