3 months ago

PHPunit - Testing a BelongsToMany relationship in a unit test

Posted 3 months ago by JeroenvanRensen

Hi everyone,

I'm creating a blog with Laravel and I'm making the tag system. I'm using PHPunit for TDD, because I really like it. I was wondering: How could I test a BelongsToMany relationship in a unit test?

Here's the part of the test I'm currently having:

/** @test */
public function a_tag_has_posts()

    $tag = factory('App\Tag')->create();

    $post = factory('App\Post')->create();

How could I test that a post has multiple tags and a tags has multiple posts.

Thank you! Jeroen

If you need more information, please ask me

Please sign in or create an account to participate in this conversation.