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

Cannnot create multiple data when call Fabricate::create() in same test case. #37

Open
otukutun opened this issue Jun 26, 2020 · 2 comments

Comments

@otukutun
Copy link

Thank you for creating great tools for testing.

I got weired behavior in this library.

When I want to create sequentional data in same model, the follwing way.

class CakeFabricateTest extends TestCase {
   /**
     * Want to create multiple data from Same Model.
     */
    public function testCreateSameModelDataInACase() {
        Fabricate::create('Users', function($data, $world) {
            return [
                'username' => 'taro',
            ];
        });
        Fabricate::create('Users', function($data, $world) {
            return [
                'username' => 'taro2',
            ];
        });

        $expected = 2;
        $actual = TableRegistry::get('Users')->find('all')->count();
        $this->assertSame($expected, $actual);
    }
}

I expected two data creating, but actually created only one data.

There was 1 failure:

1) CakeFabricate\Test\CakeFabricateTest::testCreateSameModelDataInACase
Failed asserting that 1 is identical to 2.

This is representing code. Please look at this if you want 🙏
https://github.com/otukutun/cakephp-fabricate-adaptor/tree/create-multiple-data-from-same-model

@sizuhiko
Copy link
Owner

Hi, otukutun.

Thanks reporting.

The User's id type is number.
Fabricator generates sequential value for number field by default inside each creation method scope.

In this case, I recommend usage followings:

Use number_of_generation param

    public function testCreateSameModelDataInACase() {
        $names = ['taro', 'taro2'];

        Fabricate::create('Users', 2, function($data, $world) use($names) {
            return [
                'username' => $names[$data->id - 1],
            ];
        });

        $expected = 2;
        $actual = TableRegistry::get('Users')->find('all')->count();
        $this->assertSame($expected, $actual);
    }

Use sequence

    public function testCreateSameModelDataInACase() {
        Fabricate::create('Users', function($data, $world) {
            return [
                'id' => $world->sequence('id'),
                'username' => 'taro',
            ];
        });
        Fabricate::create('Users', function($data, $world) {
            return [
                'id' => $world->sequence('id', 2),
                'username' => 'taro2',
            ];
        });

        $expected = 2;
        $actual = TableRegistry::get('Users')->find('all')->count();
        $this->assertSame($expected, $actual);
    }

@otukutun
Copy link
Author

@sizuhiko Thanks for your response!

I understand Fabricate sequence behavior 🙏

default inside each creation method scope.

Do you have a plan to change sequential value scope? In factory bot, they have sequential values globally. I think it is more useful than local scope.

REF: https://github.com/thoughtbot/factory_bot/blob/master/lib/factory_bot/sequence.rb

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

2 participants