lundi 16 septembre 2019

How in feature testing use data from factory?

In laravel 5.8 app with tests I make posting data with some dummy data, like:

$newVoteCategoryRow= [
    'id'   => null,
    'name'   => $new_vote_category_row_name,
    'meta_description'   => 'vote category meta_description on ' . now(),
    'meta_keywords'   => [ 'vote category meta_description on ' . now(), 'meta_keywords' ],
    'active'      => true,
    'in_subscriptions'      => true,
];

$response = $this->actingAs($loggedUser)->post('/admin/vote-categories', $newVoteCategoryRow);
$this->assertCount( $vote_categories_count+1, VoteCategory::all() );  

it works ok, but actually I have factory for VoteCategory table in /database/factories/VoteCategoryFactory.php, defined :

<?php

use Faker\Generator as Faker;
use \Cviebrock\EloquentSluggable\Services\SlugService;
use App\VoteCategory;

$factory->define(App\VoteCategory::class, function (Faker $faker) {

    $name= 'Vote category ' . $faker->word;
    $slug = SlugService::createSlug(VoteCategory::class, 'slug', $name);

    return [
        'name' => $name,
        'slug' => $slug,
        'active' => true,
        'in_subscriptions' => false,
        'meta_description' => $faker->text,
        'meta_keywords' => $faker->words(4),
    ];
});

and my question is if there is a way in post request instead of $newVoteCategoryRow array use my factory, not adding row in database but reading data from factory for post request ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire