momo
momo

Reputation: 984

How can I use RSpec to test my model's interaction with external services

I have a method in a model that interacts with an external video encoding service (Zencoder). It uses the zencoder_rb gem. https://github.com/zencoder/zencoder-rb

class EncodingRequest
  def check_encoding_status
    response = Zencoder::Job.details(self.request_id)
    if response.success?
      # do something
    else
      # do something else
    end
  end
end

The Zencoder::Job#details method makes a call to the Zencoder web service to find out if the video encoding is complete.

The question is how can I hijack the Zencoder::Job#details method so when it is called by check_encoding_status it will return an object that I can craft. The plan is to make that object respond to #success? in whatever way makes sense for my test.

So here's how I'd like the spec to look

it "should mark the encoding as failed if there was an error in Zencoder" do
  dummy_response = Zencoder::Response.new(:body => {:state => "finished"}, :code => 200)
  # code that will force Zencoder::Job#details to return this dummy_response

  @encoding = EncodingRequest.new(:source_asset => @final_large, :target_asset => @final_small)
  @encoding.check_encoding_status
  @encoding.status.should eql "success"
end

I am currently using rspec 2.5. I read a bit about mocks and stubs, but I am not sure it's possible to use them in this scenario.

Your help is much appreciated.

Upvotes: 2

Views: 481

Answers (1)

Andy Waite
Andy Waite

Reputation: 11096

Zencoder::Job.stub_chain(:details, :success).and_return(true)

Upvotes: 1

Related Questions