Ankit Singhaniya Ankit Singhaniya - 3 days ago 5
Ruby Question

How to mock class method in RSpec expect syntax?

I have a confusion about how to mock a class method using new

expect
syntax.
This works:

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


and this doesn't:

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


Can someone tell me what is wrong here?

Answer

Here's what you want to do (no need for double):

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