dimanche 22 novembre 2015

How to prepare same DB stubs for different unit tests in Laravel 5.1?

I'm very new at testing, so I bet the following is not correct and I feel there is a way to enhance/correct it, but I can't figure out how.

I have a Service layer that I want to test, this service layer will use one instance of a Business model, and retrieve a data set to do validations. I want the whole unit test to use the same data set which I recreate on each test method, but I bet this is not correct doing.

I would also like to have the chance to change the testing data set to run some other tests, but again, as another data set for many other test methods.

Where should I perform the initial "seeding", which is, creating a Business, associating a Service to it, and then associating a Vacancy for the Business.

If you need info about the relationships or the models, the full code is on github

So far, I've been successful to do one test, and the transaction is rolled back successfully to cleanup the DB. But by recreating the code again for another test generates duplicate key violation, and recreating in just one method, prevents me to use the test data set I need.

I hope it's clear, I may clarify whatever needed.

tests/unit/ConciergeServiceLayerUnitTest.php

use App\Business;
use App\ConciergeServiceLayer;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ConciergeServiceLayerUnitTest extends TestCase
{
    use DatabaseTransactions;

    public function testConciergeGetVacancies()
    {
        $business = factory(App\Business::class)->create();

        $service = factory(App\Service::class)->make();
        $business->services()->save($service);

        $vacancy = factory(App\Vacancy::class)->make();
        $vacancy->service_id = $service->id;

        $business->vacancies()->save($vacancy);

        $concierge = new ConciergeServiceLayer();

        $v = $concierge->getVacancies($business);
        /* ... some assertion code  */
    }

    public function testConciergeDateIsAvailable()
    {
        /* I need the same exact stub data to test another thing */
        /* I want to avoid these duplicated code */
        $business = factory(App\Business::class)->create();

        $service = factory(App\Service::class)->make();
        $business->services()->save($service);

        $vacancy = factory(App\Vacancy::class)->make();
        $vacancy->service_id = $service->id;

        $business->vacancies()->save($vacancy);

        $concierge = new ConciergeServiceLayer();

        $v = $concierge->getSomethingElse($business);
        /* ... some other assertion code  */
    }
}

database/factories/ModelFactory.php

$factory->define('App\Business', function () {
    $faker = Faker\Factory::create();
    return [
        'name' => 'HGNC',
        'slug' => 'hgnc',
        'description' => $faker->paragraph,
        'timezone' => 'America/Argentina/Buenos_Aires',
        'postal_address' => '1234 Honorio Pueyrredon, Pilar, Buenos Aires, Argentina',
        'phone' => '+542304443231',
        'social_facebook' => 'http://ift.tt/1lDbTuT',
        'strategy' => 'dateslot',
        'plan' => 'free',
        'category_id' => 1
    ];
});

$factory->define('App\Service', function () {
    $faker = Faker\Factory::create();
    return [
        'name' => 'Instalación',
        'description' => $faker->paragraph,
        'prerequisites' => $faker->paragraph,
        'duration' => 60,
    ];
});

$factory->define('App\Vacancy', function () {
    $faker = Faker\Factory::create();
    return [
        'date' => '2015-11-23',
        'start_at' => '2015-11-23 08:00:00',
        'finish_at' => '2015-11-23 09:00:00',
        'capacity' => 1
    ];
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire