2016-11-30 78 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]"})