最小化工厂的测试?

时间:2017-10-09 20:17:43

标签: php laravel laravel-5 phpunit laravel-5.4

目前我在Test类中使用了很多工厂factory(Something::class),有没有办法减少到1,所以我只能在测试方法中使用$user = factory(User::class)->create(); $token = factory(Token::class)->create([ 'user_id' => $user->id, ]); $provider = factory(Provider::class)->create([ 'user_id' => $user->id, 'token_id' => $token->id, ]); $something = factory(Something::class)->create([ 'provider_id' => $provider->id, ]); // Now test with $something

原因我使用了很多因为我必须传递一些外键。

     <section id="how-it-works">
       <div class="container">
         <div class="wizard">
           <div class="wizard-inner">
            <div class="connecting-line"></div>
              <ul class="nav nav-tabs">

                <li class="active">
                    <a href="#tab1" data-toggle="tab" aria-controls="tab1" role="tab" title="Step 1">
                        <span class="round-tab">
                            <i class="glyphicon glyphicon-folder-open"></i>
                        </span>
                    </a>
                </li>

                <li>
                    <a href="#tab2" data-toggle="tab" aria-controls="tab2" role="tab" title="Step 2">
                        <span class="round-tab">
                            <i class="glyphicon glyphicon-pencil"></i>
                        </span>
                    </a>
                </li>

                <li>
                    <a href="#tab3" data-toggle="tab" aria-controls="tab3" role="tab" title="Step 3">
                        <span class="round-tab">
                            <i class="glyphicon glyphicon-picture"></i>
                        </span>
                    </a>
                </li>

                <li>
                    <a href="#tab4" data-toggle="tab" aria-controls="tab4" role="tab" title="That's It!">
                        <span class="round-tab">
                            <i class="glyphicon glyphicon-ok"></i>
                        </span>
                    </a>
                </li>
            </ul>
        </div>

          <div class="tab-content">
            <div class="tab-pane active" id="#tab1" role="tabpanel">
                <h3>Step 1</h3>
                <p>This is step 1</p>
            </div>
            <div class="tab-pane" id="#tab2" role="tabpanel">
                <h3>Step 2</h3>
                <p>This is step 2</p>
            </div>
            <div class="tab-pane" id="#tab3" role="tabpanel">
                <h3>Step 3</h3>
                <p>This is step 3</p>
            </div>
            <div class="tab-pane" id="#tab4" role="tabpanel">
                <h3>Step 4</h3>
                <p>That's It!</p>
            </div>
          </div>
         </div>
        </div>
    </section>

1 个答案:

答案 0 :(得分:1)

您可以使用以下语法:

$factory->define(Something::class, function ($faker) {
    return [
        'title' => $faker->title,
        'content' => $faker->paragraph,
        'provider_id' => function () {
            return factory(Provider::class)->create()->id;
        }
    ];
});

$factory->define(Provider::class, function ($faker) {
    $user = factory(User:class)->create();
    return [
        'user_id' => $user->id,
        'token_id' => function () {
            return factory(Token::class)->create(['user_id' => $user->id])->id;
        }
    ];
});

然后在测试中你只能使用:

$something = factory(Something::class)->create();

请注意,根据您的需要,它可能会导致一些副作用 - 例如,在使用提供者工厂时,将始终根据您的测试创建可能正常或不正常的用户。当然,如果需要,你总是可以创建帮助方法来包装你展示的所有方法,只返回一些东西,然后在你的测试中你只能使用:

$something = $this->createSomething();