【问题标题】:Testing two forms on one page the `press` directive always submits the second form在一个页面上测试两个表单,`press` 指令总是提交第二个表单
【发布时间】:2016-04-05 16:06:47
【问题描述】:

我使用的是 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]);
}

【问题讨论】:

  • 提交按钮中是否保存了 2 个表单?能把代码生成的html放上去吗?
  • @TzookBarNoy Duh!添加一个答案,我会给你赏金。
  • 你可能因为这件小事浪费了很多时间:/
  • 批准我的回答 :)

标签: php laravel testing phpunit laravel-5.1


【解决方案1】:

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

所以当你使用

->press('Save')

他们都被点击了。

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多