0

I'm trying to test a little gem that makes downloads from youtube using 'youtube-dl'.

I want to test the output from the command youtube-dl [url] --get-title but I dont know how I do that.

This is my code:

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

And this is my test:

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

When I run the tests I get this error:

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

How do I do that?

1 Answer 1

2

Seems like your test is OK, and the implementation is failing (so, is OK for the test to report the fail)

On the implementation, Instead of using system method (which return true/false according the return code of the command), use backtick (which return the string with the output of the command)

def get_title 
  `#{YT_DL} --get-file #{get_url}`
end

ALso, as additional note, is not good for your implementation to depend on external commands (from Unit testing point of view), maybe you want to mock external system command execution (or not, you maybe know what strategy is better for your particular case)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.