2016-11-30 80 views
3

我對如何使用新的expect語法來模擬類方法存在困惑。 這工作:如何在RSpec中模擬類方法期望的語法?

Facebook 
    .should_receive(:profile) 
    .with("token") 
    .and_return({"name" => "Hello", "id" => "14314141", "email" => "[email protected]"}) 

,這並不:

facebook = double("Facebook") 
allow(facebook).to receive(:profile).with("token").and_return({"name" => "Hello", "id" => "14314141", "email" => "[email protected]"}) 

有人能告訴我這裏有什麼問題?

+2

'允許(臉譜)。爲了接收(:配置文件)。。隨着( 「令牌」)and_return({ 「名」= >「你好」,「id」=>「14314141」,「email」=>「[email protected]」})'應該可以工作 –

+0

令人驚歎的,就像一個魅力。文檔非常困難。 – aks

+0

對,嘲笑和存根並不是最簡單的主題。 –

回答

3

這裏有你想要做的(無需double)什麼:

allow(Facebook) 
    .to receive(:profile) 
    .with("token") 
    .and_return({"name" => "Hello", "id" => "14314141", "email" => "[email protected]"})