简体   繁体   English

PHP致命错误:调用未定义方法Laravel \\ Socialite \\ Contracts \\ Factory :: shouldReceive()

[英]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. 我正在尝试在应用程序中使用Facebook,Twitter和github测试我的社交身份验证。 I used Socialte and Laravel 5.1. 我使用了Socialte和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. 社交名流应使用提供程序“ facebook”接收方法“ driver”并返回某些内容。

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别名:

'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: 但是现在,您将Socialite别名为一个contract ,因此您必须模拟您的合同,如下所示:

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

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

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