Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boot events break with Event::fake() when testing #37

Open
rask opened this issue Mar 7, 2018 · 4 comments
Open

Boot events break with Event::fake() when testing #37

rask opened this issue Mar 7, 2018 · 4 comments

Comments

@rask
Copy link

rask commented Mar 7, 2018

I'm trying to test event dispatching in my application using the PHPUnit tools provided by Laravel.

In a test where Event::fake() is called before creating a new Model (a User for instance) the package's UUID trait's boot method seems to break.

I get MySQL errors relating to field id not having a default value:

QueryException: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into ...

I'm using Laravel 5.6 as my application base.

Trimmed example code to reproduce:

<?php

class migration_dis_and_dat
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();

            $table->primary('id');
        });
    }
}
<?php

class User extends Model {
    use UuidModelTrait;

    ...
}
<?php

class UserTest extends TestCase
{
    public function test_user_create_dispatches_event()
    {
        Event::fake();

        User::create(['email' => 'foo@bar.com', 'password' => Hash::make('helloworld'));

        // around here the error should appear,
        // if you comment `Event::fake()` and `Event::assertDispatched`
        // then the user should be created normally

        Event::assertDispatched(MyEvent::class);
    }
}
@JValck
Copy link
Contributor

JValck commented Mar 18, 2018

Have you tried setting the id by using the Faker library?

@rask
Copy link
Author

rask commented Mar 18, 2018

The ID is not fillable but I think I could try that for the sake of testability of the events themselves.

@vpratfr
Copy link

vpratfr commented Jul 3, 2018

Also try to report this to laravel/framework project, you may get some feedback whether this is an issue with the framework or if there are any clean solutions to it.

See also: laravel/framework#18066

@X-Coder264
Copy link

This has been fixed in laravel/framework#25185 which has been released in 5.6.34.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants