mercredi 30 mai 2018

Laravel basic test assert false.. but it's works?

I have a failure on my test suite but my code is running properly in my seeders...

When I'm running my seed once I have a failure.. But ! When I'm running twice I've not the failure on my previous dataset anymore (just on my new dataset appended). Is my function too slow to be executed ?

My feature test :

/**
     * Test if User can follow an other user and send a notification
     *
     * @return void
     */
    public function test_user_can_follow_an_other_user()
    {
        $follower = factory(User::class)->create();
        $followed = factory(User::class)->create();

        $this->actingAs($follower, 'web')
             ->post('/following', ['user' => $followed->id])
             ->assertRedirect('/following');

        $this->assertTrue($follower->follows($followed));
    }

My PhpUnit result :

PHPUnit 7.1.5 by Sebastian Bergmann and contributors.

.mpoo.F                                                                 3 / 3 (100%)

Time: 2.91 seconds, Memory: 50.00MB

There was 1 failure:

1) Tests\Feature\UserTest::test_user_can_follow_an_other_user
Failed asserting that false is true.

/Users/gtr/Code/La-dechetterie-du-web/lddw-api/tests/Feature/UserTest.php:69

FAILURES!
Tests: 3, Assertions: 9, Failures: 1.

My seeder :

public function run()
{
    $follower = User::first();
    $following = User::all();
    $followingArrayIds = [];

    foreach ($following as $u) {
        $follower->follow($u);

        array_push($followingArrayIds, [
            'follower' => $follower->id,
            'pretend_to_follow' => $u->id,
            'is '. $follower->id .' following '. $u->id .' ?' => $follower->follows($u) // should be true !!!!! 
        ]);
    }

    print_r($followingArrayIds);
}

My Model's function :

/**
     * User's following relation
     *
     * @return Relation User
     */
    public function following(){
        return $this->belongsToMany('App\User', 'followers', 'follower_id', 'following_id');
    }

    /**
     * User's followers relation
     *
     * @return Relation User
     */
    public function followers(){
        return $this->belongsToMany('App\User', 'followers', 'following_id', 'follower_id');
    }

    /**
     * Follow an other user
     *
     * @param User $user
     * @return void
     */
    public function follow(User $user){
        if($this->follows($user))
            return;

        $this->following()->attach($user);
    }

    /**
     * Check if the current user follows the user he wanted to
     *
     * @param User $user
     * @return boolean
     */
    public function follows(User $user){
        return $this->following->contains($user);
    }

Thx :)

Aucun commentaire:

Enregistrer un commentaire