In this article let’s see how we can use Factory states to write fluent test cases.

Without Factory States

First, let’s see how does our test cases looks like without any factory states.

Basically, we are writing a test to check that the user cannot view any unpublished blog posts.

tests/Feature/PostsTest.php

/**
* @test
*/
public function user_cannot_view_unpublished_post()
{
    $post = factory(Post::class)->create([
        'published_at'  => null
    ]);
    $resource = $this->get("/posts/" . $post->slug);

    $resource->assertStatus(404);
}

#laravel #testing #php #web-development

How to use Factory States to Write Fluent Laravel Test Cases
2.05 GEEK