1

My integration test works fine without a setup method. That is the factories work and data is populated into the table.

<?php

namespace tests\Integration\Model;

use App\Channel;
use App\Discussion;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ChannelModelTest extends TestCase
{

 /** @test

 */
 function it_returns_discussion_count()
 {
    // GIVEN

    factory(Channel::class, 3)->create();
    factory(Discussion::class,10)->create();

    // WHEN

   $discussion_count = Channel::find(1)->discussions->toArray();

    //THEN

    $this->assertCount(2, $discussion_count);
 }
}

but if i move the two factories in a setUp method, i get the error:
InvalidArgumentException: Unable to locate factory with name [default] [App\Channel]:

namespace tests\Integration\Model;

use App\Channel;
use App\Discussion;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ChannelModelTest extends TestCase
{
  public function setUp()
  {
    factory(Channel::class,3)->create();
    factory(Discussion::class,10)->create();
  }
 /** @test

 */
 function it_returns_discussion_count()
 {
    // GIVEN

    // WHEN

   $discussion_count = Channel::find(1)->discussions->toArray();

    //THEN

    $this->assertCount(2, $discussion_count);
 }
}

I can't understand why moving the factories into the setUp method fails.

Thanks,

Yeasir Arafat Majumder
  • 1,222
  • 2
  • 15
  • 34

1 Answers1

2

you forgot to put parent::setUp() in your setUp() method.

Usage:

public function setUp()
{
    parent::setUp();
    factory(Channel::class, 3)->create();
    factory(Discussion::class, 10)->create();
}