2016-01-15 203 views
0

我想在我的應用程序中使用Facebook,Twitter和github測試我的社交身份驗證。我使用了Socialte和Laravel 5.1。PHP致命錯誤:調用未定義的方法Laravel Socialite Contracts Factory :: shouldReceive()

這是我在測試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'); 
    } 
} 

但這從未成功運行,我不斷收到此錯誤:

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

我到處都找過了,我可以用它來的方式在我的測試中成功地模擬了Socialite,但找不到任何東西。

在我的控制器:

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

這是我試圖嘲弄。社交名流應該通過提供商'臉譜'收到方法'驅動程序'並返回一些內容。

我很確定我錯過了一些事情也許!

反饋太讚賞了!

回答

1

這應該適用於外牆。這是你的問題。

在你的應用程序配置爲你的社交別名:

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

所以,你確實可以從您的測試呼叫:

Socialite::shouldReceive(....) 

但現在,你化名社會名流到合同,所以你必須嘲笑你的合同,如下所示:

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'); 
    } 
} 
相關問題