如何在RSpec中模拟类方法期望语法?

时间:2016-11-30 12:12:13

标签: ruby-on-rails ruby rspec

我对如何使用新的expect语法模拟类方法感到困惑。 这有效:

Facebook
  .should_receive(:profile)
  .with("token")
  .and_return({"name" => "Hello", "id" => "14314141", "email" => "hello@me.com"})

并且没有:

facebook = double("Facebook")
allow(facebook).to receive(:profile).with("token").and_return({"name" => "Hello", "id" => "14314141", "email" => "hello@me.com"})

有人可以告诉我这里有什么问题吗?

1 个答案:

答案 0 :(得分:3)

这是您想要做的事情(不需要double):

allow(Facebook)
  .to receive(:profile)
  .with("token")
  .and_return({"name" => "Hello", "id" => "14314141", "email" => "hello@me.com"})