2016-04-04 59 views
0

我是phpspec(來自phpunit)的新手,我在設置由另一個模擬返回的模擬行爲時遇到問題。使用由phpspec中的另一個模擬返回的模擬

我創建了一個圍繞Guzzle客戶端的包裝類,我想檢查響應的輸出。

這裏的規格:

function it_gets_response_status_code(Client $client, Url $url, Response $response) 
{ 
    $this->beConstructedWith($client); 

    $url->__toString()->willReturn('http://example.com'); 
    $data = ['foo' => 'bar']; 

    $response->getStatusCode()->willReturn(200); 
    $client->request('POST', $url, ['form_params' => $data])->willReturn($response); 

    $this->post($url, $data); 
    assert($this->getResponseStatusCode() === 200); // Failing! :(
} 

,並在我的類中的相應功能:

public function post(Url $url, array $data) 
{ 
    $this->response = $this->client->request('POST', (string) $url, ['form_params' => $data]); 
} 

public function getResponseStatusCode() 
{ 
    return $this->response->getStatusCode(); 
} 

斷言失敗,當我檢查什麼是這個狀態代碼,我看到,而不是整數200,它是PhpSpec\Wrapper\Subject的一個實例。我在這裏錯過了什麼?

我已經搜索和谷歌搜索,但無法找到有關使用由phpspec中的另一個模擬返回的模擬資源。我想知道是否這是代碼味道?如果是這樣,我會很高興看到我可以如何做到這一點不同(目前我看不出如何保持代碼簡單,做法不同)。

回答

0

嘗試:

assert($this->getResponseStatusCode()->getWrappedObject() === 200); 

這樣的:

$response->getStatusCode()->willReturn(200) 

返回 '200' 字符串包裹在一個Subject對象,其上,如果需要,你可以然後進行模擬/存根調用。要獲得該主題的實際價值,您需要致電getWrappedObject

相關問題