在一个页面上测试两个表单`press`指令总是提交第二个表单

时间:2015-12-31 18:56:17

标签: php laravel testing phpunit laravel-5.1

我正在使用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]);
}

1 个答案:

答案 0 :(得分:2)

你的两个表格可能有一个带有文字的按钮&#34;保存&#34;

所以当你使用

->press('Save')

他们都被点击了。