2014-09-19 35 views
0

我正在测试一个使外部调用Youtube-dl二进制文件的gem,此时测试进行2个调用,一个获取视频标题和其他下载视频:如何使用rspec为命令行创建存根

拿到冠军:

def get_title 
    title = `#{YT_DL} --get-title #{get_url}` 
    title.delete("\n") 
end 

下载视频:

def download_video 
    system(YT_DL, '-o', file_path, get_url, '--no-progress') 
end 

这是我的测试:

describe Youruby do 
include FakeFS::SpecHelpers 
let(:video) {Youruby::Youtube.new('uaEJvYWc2ag')} 
context 'Youtube'do 
    describe 'get' do 
    it ".video_title" do 
     expect(video.get_title).to eq("FFmpeg-slowmotion.1") 
    end 
    end 

    describe 'download' do 
    it ".download_video" do 
     FakeFS do 
     video.download_video 
     File.exists? "#{video.get_title}-#{video.get_id}.mp4" 
     end 
    end 
    end 
end 

我如何让这个电话的存根?谢谢。

回答

2

你可能想看看Excon或VCR gems,它可以让你存根HTTP请求。

+0

我正在使用youtube-dl [链接](http://rg3.github.io/youtube-dl/)可以使用Excon或VRC存根他的电话吗? – 2014-09-19 18:43:42

+0

是的。例如,VCR(https://www.relishapp.com/vcr/vcr/v/1-6-0/docs/test-frameworks/usage-with-rspec)具有“记录”模式,可让您将查询记录到实时网址。然后您可以使用录音(卡带)进行后续测试。 – AndyV 2014-09-19 20:30:03

+0

我应该补充说这些解决方案在不同的层面上工作。你的代码认为它正在发送实时请求。 Excon/VCR拦截呼叫并返回可以控制的响应。这不是在你的代码中存根,而是在一个更高的层次上,它允许你的代码认为它与真实的url相互作用。 – AndyV 2014-09-19 20:31:45