views:

40

answers:

0

Hi there,

I've been trying to mock a rest-client nested resource to test my class. Here's the test:

require 'rest_client'

require File.join(File.dirname(__FILE__), "/../src/job_resource.rb")


describe JobResource, "#has_stagein" do
  before :each do
    response = mock(String).stub!(:code).and_return(200)
    job_resource = mock(RestClient::Resource).as_null_object
    stagein_resource = mock(RestClient::Resource).as_null_object
    stagein_resource.stub!(:head).and_return(response)
    job_resource.stub!(:[]).and_return(stagein_resource)
    @job = JobResource.new(job_resource, 1)
  end
  it "should return true" do
    @job.has_stagein.should be_true
  end
end

And the production code:

class JobResource
  def initialize(resource, id)
    @id = id
    @resource = resource
  end
  def has_stagein
    resp = @resource['stagein'].head
    resp.code == 200
  end
end

But when I run the spec I get:

1)
NoMethodError in 'JobResource#has_stagein should return true'
undefined method `code' for #<Proc:0x7f63d5f43040>
[..]/spec/../src/job_resource.rb:8:in `has_stagein'
[..]/spec/job_resource_spec.rb:16:

So, shouldn't it be passing? Why isn't code being stubbed?