开发者

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

开发者 https://www.devze.com 2023-03-09 07:29 出处:网络
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

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 开发者_Python百科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.


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

精彩评论

暂无评论...
验证码 换一张
取 消