When trying to call the press()
method, I always get
InvalidArgumentException: Unreachable field ""
at that line.
According to the docs:
"Press" a button with the given text or name.
My method:
press('Create')
and my button is
<button class="btn btn-lg btn-primary" type="submit" name="submit">Create</button>
I have also tried to use the name
with the same result.
I have also tried submitForm('Create')
which works, but then seeInDatabase('employees', ['email' => '[email protected]'])
always fails.
Unable to find row in database table [employees] that matched attributes [{"email":"[email protected]"}].
Here is my full method
public function testExample()
{
$this->actingAs(\App\Employee::where('username', 'grant')->first())
->visit('/employees/create')
->type('john', 'username')
->type('1234', 'password')
->type('[email protected]', 'email')
->submitForm('Create')
->seeInDatabase('employees', ['email' => '[email protected]'])
->see('Employee Directory');
}
UPDATE 1
Here is my form:
{!! Form::open(['route' => 'employees.store', 'method' => 'POST']) !!}
@include('employees.form')
{!! Form::close() !!}
As you can see, there's no way my fields are outside of the form.
I'm not sure what the exact cause of your problem is but it could be a couple of things. I would have commented but I don't have enough reputation yet.
Firstly, have you run your plain html trough a validator? If it gives you any syntax errors try to fix them and run the unit test again. (You can try this one https://validator.w3.org/, just copy and paste your html)
Another cause may be that you have some javascript that modifies your DOM and Laravel testing can't handle that.
And lastly, you can try to set the 'value' attribute of your <button>
to "Create"
<button class="btn btn-lg btn-primary" type="submit" name="submit" value="Create">Create</button>
Or change the <button>
to an <input>
with type
attribute "submit"
and value
to "Create"
<input type="submit" class="btn btn-lg btn-primary" name="submit" value="Create" />
From experience, unreachable field errors are normally caused by the buttons being outside of the <form>
element, i've got a similair scenario where another section of the page is passed to the controller via $request
but because it's not within the same immediate form element as everything else, PHPUnit can't assosciate it with the form.
It could be worth changing the method from press()
to click()
which just looks for something it can click on in general rather than attempting to match a submit button to the form.
As mentioned above as well, PHPUnit can't test JavaScript by itself (if you're using Javascript of course), you need something like Selenium to do that.
Without the markup it's quite hard to say, could you post the markup of your form?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With