PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] Laravel persisting only during duration of unit test

 January 19, 2022     laravel, laravel-5, phpunit, unit-testing     No comments   

Issue

I am doing a unit test of an API endpoint, I have something like this:

public function testPostService()
{
    $response = $this->json('POST', '/api/v1/service',[
            "id"=> "someId",
            "number" => 123
    ]);
    $response
        ->assertStatus(201)
        ->assertJson([
            'created' => true,
        ]);
}

id is a unique value, which leads me to the problem each time I run the tests. I have to either empty the database or change the id value.

Is there any way that the persisted values are only kept during the duration of the test?

My phpunit.xml is the standard


Solution

There's a trait called RefreshDatabase which refreshes the database after each test made. Just add it to the class of your test cases' class and you're good to go.

Does this solve your problem?



Answered By - Dan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing