0

Using Mocha, I'm stubbing the same method that needs to return 2 separate values. No matter what I do, it only returns 1 of the 2 values, thus 1 of my rspec tests always fail. How do I get the stub to return the correct value at the right time?

The code:

describe "#method" do
  it "has something" do
    hash = { "allow_sharing" => "1"}
    CustomClass.stubs(:app_settings).returns(hash)
    get 'method', :format => :json
    JSON.parse(response.body).count.should eq(1)
  end
  it "does not have something" do
    hash = { "allow_sharing" => "0"}
    CustomClass.stubs(:app_settings).returns(hash)
    get 'method', :format => :json
    JSON.parse(response.body).count.should eq(0)
  end
end

I also tried it this way with a before block. Still no luck.

describe "#method" do
  before do
    hash = { "allow_sharing" => "1"}
    CustomClass.stubs(:app_settings).returns(hash)
  end
  it "has something" do
    get 'method', :format => :json
    JSON.parse(response.body).count.should eq(1)
  end
# ... etc.
Vega
  • 27,856
  • 27
  • 95
  • 103
David Nix
  • 3,324
  • 3
  • 32
  • 51
  • Have you tried calling `CustomClass.app_settings` immediately after stubbing just to verify it's returning the expected hash? This might help narrow the problem down and eliminate any doubt that something else is causing your response to have an unexpected count. – exbinary Feb 13 '13 at 02:41
  • an4rcho, your suggestion did the trick. The stubbing was working correctly. The problem was in the `get 'method'` itself. It was not checking the value correctly. – David Nix Feb 16 '13 at 21:06

1 Answers1

1

try using as_null_object if thats available. so for example for all lines with stubs:

CustomClass.stubs(:app_settings).returns(hash).as_null_object
TakaGoto
  • 362
  • 4
  • 17