fokosun
fokosun

Reputation: 546

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!

Upvotes: 2

Views: 1846

Answers (1)

schellingerht
schellingerht

Reputation: 5796

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

Upvotes: 1

Related Questions