Laravel 基本测试 assert false.. 但它有效吗?

标签 laravel testing eloquent

我的测试套件出现故障,但我的代码在我的播种机中正常运行......

当我运行我的种子时,一旦我失败了.. 但是!当我运行两次时,我以前的数据集不再失败(只是在我附加的新数据集上)。我的功能是否太慢而无法执行?

我的功能测试:

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

谢谢 :)

最佳答案

使用 fresh() 获取 $follower 的新副本应该可以解决问题:

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

关于Laravel 基本测试 assert false.. 但它有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50607926/

相关文章:

php - Cucumber响应对象——PHP环境

php - Laravel4 Eloquent 中的连接和动态 `Where`

php - 如何在Laravel中过滤每个projectId的taskId?

php - 使用未定义的常数触点-假定'contacts'错误

ruby - 使用 xpath 与 span 获取元素(一个适用于 IE 和 Firefox,但另一个仅适用于 IE)

python - Ruby 的 VCR 库是否有 python 替代品?

php - 在 Laravel 5.2 中为所有 Eloquent 模型添加全局方法

javascript - Laravel + Angular - 页面未加载

mysql - 查询异常(23000)SQLSTATE [23000] : Integrity constraint v1452

laravel - 如果使用 whereRaw(),我应该如何清理 Laravel 查询?