Home>
Problem

class ArticleTestusesuse DatabaseTransactions;.
It is a function that automatically discards the data stored in the DB after running phpunit with laravel.
In other test codes, the data disappears after running the test firmly.
class ArticleTestalso defines it, but data does not disappear from DB after running the test.

What I tried below

:useis defined in function → It was an error in the first place.
: I think that dataProvider is related, comment out there and execute → result does not change
:php artisan config: clearto clear the cache → No particular change.
:useI tried to change the position->There was no particular change and there was no problem.
There are other solutions that I came up with, but for now I don't know why the data is not destroyed.

Problems
<? php
namespace Tests \ Unit;
use Tests \ TestCase;
use Illuminate \ Foundation \ Testing \ WithoutMiddleware;
use Illuminate \ Foundation \ Testing \ DatabaseTransactions;
use Auth;
use App \ User;
use Illuminate \ Support \ Facades \ Validator;
use App \ Article;
use App \ Http \ Requests \ ArticleCreateFormRequest;
use Faker \ Generator as Faker;

class ArticleTest extends TestCase
{
    // This doesn't work
    use DatabaseTransactions;
    / **
     * @group exa
     * /
    public function testNewArticleSend ()
    {
        $falseAccess = $this->get ('/ articles/create');
        $falseAccess->assertStatus (302);
        $user = factory (User :: class)->create ();
        $trueAccess = $this->actingAs ($user)->get ('/ articles/create');
        $trueAccess->assertStatus (200);
        $table = 'article';
        $article = factory (Article :: class)->create ();
        $this->assertDatabaseHas ($table, $article->toArray ());
    }
    / **
     * @dataProvider dataProviderNewArticle
     * @group exa
     * /
    public function testExample ($dataList, $expect)
    {
        $request = new ArticleCreateFormRequest ();
        $rules = $request->rules ();
        $validator = Validator :: make ($dataList, $rules);
        $result = $validator->passes ();
        $this->assertEquals ($expect, $result);
    }public function dataProviderNewArticle () {
        $faker = \ Faker \ Factory :: create ();
        return [
            [
                [
                    'articleTitle' =>$faker->sentence (),
                    'articleDescription' =>$faker->sentence (),
                    'articleBody' =>$faker->userName (),
                    'authorId' =>$faker->numberBetween (8, 10),
                    'typeId' =>$faker->numberBetween (1, 2),
                    'categoryId' =>$faker->numberBetween (10, 17),
                    'tagIds' =>$faker->numberBetween (11, 19),
                    'postDate' =>$faker->date (),
                    'postTime' =>$faker->time (),
                    'articleThumbnail' =>$faker->userName ()
                ],
                true
            ],
            [
                [
                    'articleTitle' =>'',
                    'articleBody' =>'',
                    'articleDescription' =>'',
                    'articleBody' =>'',
                    'authorId' =>'',
                    'typeId' =>'',
                    'categoryId' =>'',
                    'tagIds' =>'',
                    'postDate' =>'',
                    'postTime' =>'',
                    'articleThumbnail' =>'',
                ],
                false
            ],
            [
                [
                    'articleTitle' =>str_repeat ('a', 256),
                    'articleBody' =>'',
                    'articleDescription' =>str_repeat ('a', 256),
                    'articleBody' =>'',
                    'authorId' =>'',
                    'typeId' =>'',
                    'categoryId' =>'',
                    'tagIds' =>'',
                    'postDate' =>str_repeat ('a', 256),
                    'postTime' =>'',
                    'articleThumbnail' =>str_repeat ('a', 256)
                ],
                false
            ],
        ];
    }
}
  • Answer # 1

      

    class ArticleTest uses the function Use DatabaseTransactions;
      It is a function that automatically discards the data stored in the DB after running phpunit with laravel.

    I always useRefreshDatabase.
    Conversely, since I did not useDatabaseTransactions, I examined it, butRefreshDatabaseseems to be a new function.
    If it is 5.6, it seems better to use this.

    https://readouble.com/laravel/5.5/en/database-testing.html

    https://qiita.com/Frog_woman/items/6a143af0a042dc853e88

      

    class ArticleTest also defines it, but data does not disappear from DB after running the test.

    DatabaseTransactionsI don't know the internal behavior of the trait, so it may not be appropriate, but for the time being, make sure thatdownin the migration file is properly defined Please try it.

    If you don't know why the trait isn't working, you may want to writerefreshto thesetUpfunction as shown below.

    https://qiita.com/kuriya/items/4c9dbefc19514f415374

    public function setUp () {
        parent :: setUp ();
        Artisan :: call ('migrate: refresh');
        Artisan :: call ('db: seed');
    }

    The following issue may be helpful.

    https://github.com/laravel/framework/issues/10873