代码之家  ›  专栏  ›  技术社区  ›  Daedelus

LARAVEL BASIC TEST ASSERT FALSE..但它有效吗?

  •  0
  • Daedelus  · 技术社区  · 8 年前

    我的测试套件失败了,但我的代码在播种机中运行正常…

    当我有一次失败的时候…但是!当我运行两次时,我的前一个数据集不再出现故障(只是附加了新的数据集)。我的功能是否太慢而无法执行?

    我的功能测试:

    /**
         * 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));
        }
    

    我的phpunit结果:

    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.
    

    我的播种机:

    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);
    }
    

    我的模型的功能:

    /**
         * 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:)

    1 回复  |  直到 8 年前
        1
  •  2
  •   maxwilms    8 年前

    获取的新副本 $follower 具有 fresh() 应该这样做:

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