Laravel:功能测试由于添加了中间件而失败

时间:2020-04-05 20:31:18

标签: laravel phpunit

用户注册并验证电子邮件后,他们必须使用其他信息完成注册。这发生在/register/complete-signup

这个问题对我来说绝对没有道理。

无论出于何种原因,当我添加中间件has-not-completed-signup时,测试都会开始失败,因为App \ User不再具有通过attach()在控制器中发生的关联App \ Account

一旦我从路由中删除了中间件,它就可以正常工作。

我的中间件在那里,可以防止已经完成注册的用户访问或张贴到那些路由。我在浏览器中进行了测试,并且重定向有效。测试中正在使用控制器方法,我可以dd($account->users)并获得正确的响应。但是,如果我做$user->accounts,则收藏集为空。

删除中间件后,$user->accounts将不再为空。但是我在中间件中做了一个dd(),它甚至没有运行(这是正确的,因为用户没有帐户)。

那为什么会失败呢?我完全迷路了。

我试图在下面包括所有相关信息。如果您还有其他需要,请告诉我。

编辑:

在我的中间件中,我已经注释掉了该功能。关于检查雄辩的关系会使我的测试失败。我不知道为什么。

这会使测试失败:

if (!auth()->user()->accounts->isEmpty()) {      
  //return redirect(RouteServiceProvider::HOME);
}

例如,如果我将其更改为这样的无用的东西,它将起作用:

if (auth()->user()) {
//return redirect(RouteServiceProvider::HOME);
}

我可以做$account->users,但是当我使用中间件时,$user->accounts会在控制器上返回空集合


原文:

这是我的路线:

// auth scaffolding
Auth::routes(['verify' => true]);

// main app routes
Route::middleware('verified', 'auth')->group(function() {

    // User verified and has an App\Account
    Route::middleware('completed-signup')->group(function() {
        Route::get("/", 'HomeController@index' )->name('home');
        Route::get('/paywall', 'BillingController@paywall')->name('paywall');

    });

    // The user hasn't attached an App\Account to their User
    Route::middleware('has-not-completed-signup')->group(function() {
        Route::get("/register/complete-signup", 'AccountController@showCompleteSignup' )->name('complete-signup');
        Route::post('/register/complete-signup', 'AccountController@storeCompleteSignup')->name('complete-signup.store');
    });

});

has-not-completed-signup中间件:

    public function handle($request, Closure $next)
    {
        if (auth()->user()->hasCompletedAccountSetup()) {

            return redirect(RouteServiceProvider::HOME);
        }

        return $next($request);
    }

应用/用户方法:

Class User extends Authenticatable implements MustVerifyEmail { 
...

    public function accounts() {
        return $this->belongsToMany('App\Account', 'account_role_user')->withPivot('role_id');
    }
    public function hasCompletedAccountSetup() {
        return !$this->accounts->isEmpty();
    }
    ...

AccountController @ storeCompletedSignup:

    public function storeCompleteSignup(Request $request) {

        $validatedData = $request->validate([
            'company' => 'required|max:255',
            'contact_state' => 'required|max:255',
            'contact_country' => 'required|max:255',
            'contact_zip' => 'required|max:255',
            'contact_city' => 'required|max:255',
            'contact_phone' => 'nullable|max:255',
            'contact_address_1' => 'required|max:255',
            'contact_address_2' => 'nullable|max:255',
            'contact_first_name' => 'required',
            'contact_last_name' => 'required',
            'contact_email' => 'required'
        ]);

        $user = auth()->user();

        $account = new Account($validatedData);

        $account->contact_first_name = $user->first_name;
        $account->contact_last_name = $user->last_name;
        $account->contact_email = $user->email;

        $account->save();



        $account->users()->attach(
            $user->id, 
            ['role_id' => Role::where('name', 'owner')->first()->id ]
        );

        return $request->wantsJson()
                    ? new Response('Signup Completed Successfully', 201)
                    : redirect()->route('/');

    }

我的测试:


    /**
     * @test
     */
    public function a_user_can_complete_signup()
    {


        $user = Factory('App\User')->create();
        $this->actingAs($user);

        $accountAttributes = factory('App\Account')->raw([
            'contact_first_name' => "TEST",
            'contact_last_name' => $user->last_name,
            'contact_email' => $user->email,
            'contact_country' => "USA"
        ]);

        $res = $this->post('/register/complete-signup', $accountAttributes);

        $res->assertSessionDoesntHaveErrors();

        $this->assertTrue( !$user->accounts->isEmpty() ); // THIS FAILS
        $this->assertTrue( $user->accounts->first()->company == $accountAttributes['company']);
        $this->assertTrue( $user->accounts->first()->contact_first_name == $user->first_name );


    }

1 个答案:

答案 0 :(得分:0)

问题实际上与中间件无关,但这是因为我必须在测试后的POST之后刷新模型。

$this->assertTrue( !$user->accounts->isEmpty() );

需要成为

$this->assertTrue( !$user->fresh()->accounts->isEmpty() );

通过了测试。

我知道freshrefresh()方法,但是引起问题的中间件对我来说没有意义。