我正在尝试测试一个可以使用' youtube -dl‘从youtube下载的小gem。
我想测试命令youtube-dl [url] --get-title的输出,但我不知道如何做到这一点。
这是我的代码:
module Youruby
class Youtube
YT_DL = File.join(File.expand_path(File.dirname(__FILE__)), "../bin/youtube-dl")
def initialize(id)
@id = id
end
def get_title
system(YT_DL, '--get-title', get_url)
end
end
end这是我的测试:
require "spec_helper"
require "youruby"
describe Youruby do
it "get video title" do
video = Youruby::Youtube.new('uaEJvYWc2ag')
video.get_title.should == "FFmpeg-slowmotion.1"
end
end当我运行测试时,我得到这个错误:
Failure/Error: video.get_title.should == "FFmpeg-slowmotion.1"
expected: "FFmpeg-slowmotion.1"
got: true (using ==)
Diff:
@@ -1,2 +1,2 @@
-"FFmpeg-slowmotion.1"
+true我该怎么做?
发布于 2014-09-17 03:21:46
看起来您的测试是正常的,并且实现正在失败(所以,测试报告失败是可以的)
在实现上,不使用system方法(根据命令的返回代码返回true/false ),而使用backtick (使用命令的输出返回字符串)
def get_title
`#{YT_DL} --get-file #{get_url}`
end作为补充说明,ALso不利于您的实现依赖于外部命令(从单元测试的角度来看),也许您想模拟外部系统命令的执行(或者不是,您可能知道哪种策略更适合您的特定情况)
https://stackoverflow.com/questions/25876608
复制相似问题