1

i want to simply test my model to see if it can be created or not here is what i tried :

    public function it_test_insert_model(){
//        $this->actingAs(User::class);
        $wish = factory(Model::class)->create();
        $this->post('/model/create',$wish->toArray());
        $this->assertEquals(1,Model::all()->count());
    }

now my problem is every time it fails because it should equal the the numbers of models i have in my table . is there any way to just create one and test if it was created and then remove it ??? because now every time it remains in database . i just want to test if the model is able to be created or not . thanks

1

1 Answer 1

3

There is two approaches there is commonly used, you can either run migrations or database transactions to reset your database state. Migrations runs fairly slow if your application is big, transactions is way quicker and is my suggested approach.

Database migrations

use Illuminate\Foundation\Testing\DatabaseMigrations;

class YourTestClass {
    use DatabaseMigrations;

    public function it_test_insert_model() {
        ...
    }
}

Database transactions

use Illuminate\Foundation\Testing\DatabaseTransactions;

class YourTestClass {
    use DatabaseTransactions;

    public function it_test_insert_model() {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.