简体   繁体   中英

PHP Fatal error: Call to undefined method Laravel\Socialite\Contracts\Factory::shouldReceive()

I am trying to test my social authentication with facebook, twitter and github in my application. I used Socialte and Laravel 5.1.

Here is my attempt at testing socilate:

use Laravel\Socialite\Contracts\Factory as Socialite;

class AuthTests extends TestCase
{
    public function testFb()
    {
        Socialite::shouldReceive('driver')->once()->with('facebook')->andReturn('code');
        $this->visit('/auth/login/facebook');
    }
}

But this never runs successfully, i keep getting this error:

[Symfony\Component\Debug\Exception\FatalErrorException]Call to undefined method Laravel\Socialite\Contracts\Factory::shouldReceive()

I have looked all over for ways that i can use to successfully mock Socialite in my tests but couldn't find any.

In my controller:

private function getAuthorizationFirst($provider)
{
    return $this->socialite->driver($provider)->redirect();
}

This is what i was trying to mock. Socialite should receive the method 'driver' with provider 'facebook' and return something.

I am pretty sure i have missed out a couple of things maybe!

feedback much appreciated!

This should work for facades. And there's your problem.

In your app config is your Socialite alias:

'Socialite' => Laravel\Socialite\Facades\Socialite::class

So you can indeed call from your test:

Socialite::shouldReceive(....)

But now, you aliased Socialite to a contract , so you have to mock your contract, like so:

class AuthTests extends TestCase
{
    private $socialiteMock;

    public function setUp()
    {
        parent::setUp();
        $this->socialiteMock = Mockery::mock('Laravel\Socialite\Contracts\Factory');
    }

    public function testFb()
    {
        $this->socialiteMock
            ->shouldReceive('driver')
            ->once()
            ->with('facebook')
            ->andReturn('code');
        $this->visit('/auth/login/facebook');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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