laracasts / TestDummy

Easy factories for PHP integration testing.
https://laracasts.com/lessons/whats-new-in-testdummy
MIT License
455 stars 80 forks source link

Overrides don't work with relations #77

Closed bobbybouwmann closed 9 years ago

bobbybouwmann commented 9 years ago

So here is the issue. I have a user which has one role (role_id). When I create the user with TestDummy with overrides it gives an error while it creating the associated role!

This is my setup

// tests/factories/factories.php

$factory('App\User', [
    'name' => $faker->name,
    'email' => $faker->email,
    'password' => $faker->word,
    'role_id' => 'factory:Sebwite\Entry\Models\Role',
    'created_at' => $faker->date(),
    'updated_at' => $faker->date()
]);

$factory('Sebwite\Entry\Models\Role', [
    'name' => $faker->name,
    'display_name' => $faker->name,
    'description' => $faker->sentence(),
    'created_at' => $faker->date(),
    'updated_at' => $faker->date()
]);

Note that the Role model is from a package, but I think it shouldn't matter since it's namespaced correctly.

And now the test part

// tests/_support/FunctionalHelper

 public function signIn()
{
        $email = 'foo@example.com';
        $password = 'password';
        $this->haveAnAccount(compact('email', 'password'));

        $I = $this->getModule('Laravel5');

        $I->amOnPage('/login');
        $I->fillField('email', $email);
        $I->fillField('password', $password);
        $I->click('Login');
}

public function haveAnAccount($overrides = [])
{
        return TestDummy::create('App\User', $overrides);
}

This is the error

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table roles has no column named password (SQL: insert into "roles" ("name", "display_name", "description", "created_at", "updated_at", "email", "password") values (Vince Graham, Camila Mayert, Quis molestiae laboriosam id ea expedita., 2010-11-30 00:00:00, 1998-08-18 00:00:00, foo@example.com, password))

As you can see in the error it tries to insert the overrides for the User model in the Role model, while that shouldn't be happening!

If you need anything else please let me know!

bobbybouwmann commented 9 years ago

So what I did was remove my vendor directory and installed everything again with composer install. It's working now.

bobbybouwmann commented 9 years ago

So after creating some more tests it started happening again, is this a real bug or am I doing something wrong?

0xuhe commented 9 years ago

Hi, I got the same issue with you.And reinstall the vendor by composer seems didn't work for me

mattkoch614 commented 9 years ago

I also just started experiencing this behavior. Just as an FYI, if you fall back to 2.0.12 for now it starts working again:

    "require-dev": {
        "phpspec/phpspec": "~2.1",
        "codeception/codeception": "~2.0",
        "laracasts/testdummy": "2.0.12"
    },
bobbybouwmann commented 9 years ago

composer update to TestDummy 2.1.2 fixed it for me!