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

标签 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]);
}

最佳答案

您的两个表单可能有一个带有文本“保存”的按钮

所以当你使用

->press('Save')

它们都被点击了。

关于php - 在一页上测试两个表单 `press` 指令总是提交第二个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34550588/

相关文章:

php - Laravel findorfail() 重定向

javascript - 如果我使用 jQuery,我应该进行跨浏览器测试吗?

php - 在 Laravel 5 命令中使用构造函数参数时出现 BindingResolutionException

php - 如何以自定义顺序运行迁移

php - 几秒钟后 Laravel 自动注销?

python - Django测试客户端-发送POST数据返回400错误

java - 输出为文本墙时的单元测试

php - 两个 Php sql 查询 avgs 会得到相同的结果。为什么?

java - Android Studio 登录 创建帐户

php - 如何获取具有相同 ID 的选定文档的当前评论?