php - Laravel 5.1 PHPUnit - press() 给我 'Unreachable field ""'

标签 php laravel testing phpunit laravel-5.1

尝试调用 press() 时方法,我总是得到

InvalidArgumentException: Unreachable field ""

在那条线上。

根据文档:

"Press" a button with the given text or name.

我的方法:

press('Create') 

我的按钮是

<button class="btn btn-lg btn-primary" type="submit" name="submit">Create</button> 

我也尝试过使用 name结果相同。

我也试过submitForm('Create')哪个有效,然后seeInDatabase('employees', ['email' => 'john@email.com'])总是失败。

Unable to find row in database table [employees] that matched attributes [{"email":"john@email.com"}].

这是我的完整方法

public function testExample()
{

  $this->actingAs(\App\Employee::where('username', 'grant')->first())
    ->visit('/employees/create')
    ->type('john', 'username')
    ->type('1234', 'password')
    ->type('john@email.com', 'email')
    ->submitForm('Create')
    ->seeInDatabase('employees', ['email' => 'john@email.com'])
    ->see('Employee Directory');
}

更新 1

这是我的表格:

{!! Form::open(['route' => 'employees.store', 'method' => 'POST']) !!}
  @include('employees.form')
{!! Form::close() !!}

如您所见,我的字段不可能位于表单之外。

最佳答案

我不确定您的问题的确切原因是什么,但可能有多种原因。 我会发表评论,但我还没有足够的声誉。

首先,您是否通过验证器运行了纯 html?如果它给您任何语法错误,请尝试修复它们并再次运行单元测试。 (你可以试试这个 https://validator.w3.org/ ,只需复制并粘贴你的 html)

另一个原因可能是您有一些修改 DOM 的 javascript,而 Laravel 测试无法处理它。

最后,您可以尝试设置 <button> 的“值”属性去“创造”

<button class="btn btn-lg btn-primary" type="submit" name="submit" value="Create">Create</button>

或更改 <button><input>type属性 "submit"value"Create"

<input type="submit" class="btn btn-lg btn-primary" name="submit" value="Create" />

关于php - Laravel 5.1 PHPUnit - press() 给我 'Unreachable field ""',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010792/

相关文章:

php - 使用 artisan 迁移 Laravel 数据库

php - 如何按组 ID 计算 php 中 SQL 列的平均值并显示在 HTML 页面上?

php - codeigniter 上传 Excel 并在 View 中显示内容

php - WordPress 二十四 : Make separation between posts thicker

php - Model::create($dataarray) 删除随机数据行

Laravel 查询 - 获取最畅销的商品

php - UTF-8贯穿始终

c++ - 如何测试用户的代码?

mysql - 验证 2 个数据库的免费工具

ruby-on-rails - cucumber + Selenium 随机失败