简体   繁体   English

尝试模拟Laravel Socialite Google登录进行集成测试

[英]Trying to mock Laravel Socialite Google Login for integration testing

I am trying to mock Laravel Socialite to test Google oAUTH Login using the guide here . 我正在尝试使用此处的指南模拟Laravel Socialite以测试Google oAUTH登录。

use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\GoogleProvider;
use Laravel\Socialite\Two\User as SocialUser;

public function mock_socialite ($email = 'foo@gmail.com', $token = 'foo', $id = 1)
    {
        // create a mock user
        $socialiteUser = $this->createMock(SocialUser::class);
        $socialiteUser->token = $token;
        $socialiteUser->id = $id;
        $socialiteUser->email = $email;

        // create a mock provider of the user
        $provider = $this->createMock(GoogleProvider::class);
        $provider->expects($this->any())
            ->method('user')
            ->willReturn($socialiteUser);

        // create a mock Socialite instance
        $stub = $this->createMock(Socialite::class);
        $stub->expects($this->any())
            ->method('driver')
            //->with('google')
            ->willReturn($provider);

        // Replace Socialite Instance with our mock
        $this->app->instance(Socialite::class, $stub);
    }

However, I am getting the below error: 但是,我收到以下错误:

Trying to configure method "driver" which cannot be configured 
because it does not exist, has not been specified, is final, or is static

I checked and found that driver() method does exist in Illuminate\\Support\\Manager (from where Socialite is extended) and this method is a public method. 我检查发现, Illuminate\\Support\\ManagerSocialite的扩展位置)中确实存在driver()方法,并且该方法是公共方法。 Not sure why am I getting this error. 不知道为什么会收到此错误。

Socialite is a facade and often their integration is far away from how you would use it, therefor it does not have the driver method. 社交名媛是一个门面,通常它们的集成与您的使用方式相去甚远,因此它没有驱动程序方法。 You are including that facade to mock it, this is not the approach he has in the guide. 您要包括模拟该外观的外观,这不是他在指南中所采用的方法。 So replacing the facade with the use statement he uses, would probably solved your problems. 因此,用他使用的use语句替换外观,可能会解决您的问题。

use Laravel\Socialite\Contracts\Factory as Socialite;

Instead of. 代替。

use Laravel\Socialite\Facades\Socialite;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM