1

I would like to test my sign up form in Laravel. I'm doing this:

$this->visit('/auth/signup/free')
        ->type('First Name', 'first_name')
        ->type('[email protected]', 'email')
        ->type('mypassword', 'password')
        ->press('Get Started');

But I have an error 500, because I then redirect the user to a welcome page which test some property of the current user, and these ones doesn't exist:

A request to [http://localhost/welcome] failed. Received status code [500].
Caused by
exception 'ErrorException' with message 'Undefined property: App\Models\User::$active' in D:\workspace\myproject\app\Models\User.php:48
Stack trace:
#0 D:\workspace\myproject\app\Models\User.php(48): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined prope...', 'D:\\workspace\\my...', 48, Array)
#1 D:\workspace\myproject\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(2670): App\Models\User->active()
[...]

The user appears in the database, but it seems that Laravel doesn't have access to any of its property.

How could I handle this?

Edit: I'm using Laravel 5.3. I have the same error than this guy: https://laracasts.com/discuss/channels/testing/51-phpunit-tests-fail-without-middleware/replies/89708

2
  • What version of Laravel are you using? Commented Jan 31, 2017 at 23:12
  • I'm using Laravel 5.3. Commented Feb 1, 2017 at 12:34

2 Answers 2

2

Let me guess. You are probably observing error like accessing /welcome without authentication. Check if /welcome page gives such an error for an unauthorized request.

Googling says (it is marked as a solution) that Laravel can have problem with encrypting cookies when testing. Try to disable EncryptCookies middleware for testing.

Dusk in Laravel 5.4 does behave like a real browser, it will probably help you.

Sign up to request clarification or add additional context in comments.

1 Comment

I have a middleware on this page that redirects the user if not connected, so this appear should never appear. I tried to disable this middleware without success. The user is redirect to the welcome page because the we click on the "sign up" button, and we create the user, sign in him and redirect him directly.
0

Updated

Actually, it depends on the Laravel version.

If you are using the latest version (5.4), then I think you inverted you key => value. Instead of

->type('[email protected]', 'email')

Do

->type('email', '[email protected]')

For all of them.

Then, somewhere you need to assert something to see if the user was redirected to home page properly. e.g: ->assertPathIs('/home');

If you are on 5.3, then your key values are alright. Just add an assertion at the end to see. Like: ->seePageIs('/welcome');

4 Comments

Laravel docs say ->type('Taylor', 'name'). So he uses it correctly.
I guess, he uses 5.3 or older, not dusk.
You are right, I just updated my answer. @shukshin.ivan, if you can help him, just put an answer, I will just delete mine if it doesnt help the OP.
I'm using 5.3, the order is correct and the user is well redirected. Just when redirected I got this error and can't do any assert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.