Skip to content

Commit

Permalink
Add test for Addic7ed::Episode#download_best_subtitle!
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbaudino committed Jan 5, 2014
1 parent 90beb58 commit 0646ea9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/addic7ed/episode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def follow_redirection(lang, new_uri, http_redirect_limit)
end

def save_subtitle(content)
open "#{filename}".gsub(/\.\w{3}$/, '.srt'), 'w' do |f|
Kernel.open "#{filename}".gsub(/\.\w{3}$/, '.srt'), 'w' do |f|
f << content
end
rescue
Expand Down
37 changes: 36 additions & 1 deletion spec/addic7ed-episode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,42 @@

describe '#download_best_subtitle!' do

it 'should be tested, but I\'m not sure how to do it properly'
before :each do
WebMock.reset!
stub_request(:get, 'http://www.addic7ed.com/serie/The_Walking_Dead/3/2/8')
.to_return File.new('spec/responses/walking-dead-3-2-8.http')
stub_request(:get, 'http://www.addic7ed.com/original/68018/4')
.to_return File.new('spec/responses/walking-dead-3-2-8_best_subtitle.http')
# The Episode object must be re-created between every test, since redirection may modify its URI
@reset_episode = Addic7ed::Episode.new(@filename)
# Prevent actual disk writing
Kernel.stub(:open)
end

it 'should get the best subtitle candidate' do
Addic7ed::Episode.stub(:best_subtitle).once
@reset_episode.download_best_subtitle!('fr')
end

it 'should be called recursively' do
stub_request(:get, 'http://www.addic7ed.com/original/68018/4').to_return File.new('spec/responses/basic_redirection.http')
stub_request(:get, 'http://www.addic7ed.com/original/68018/4.redirected').to_return File.new('spec/responses/walking-dead-3-2-8_best_subtitle.http')
Addic7ed::Episode.stub(:download_best_subtitle!).twice.and_call_original
@reset_episode.download_best_subtitle!('fr')
end

it 'should raise HTTPError when stuck in a HTTP redirections loop' do
stub_request(:get, 'http://www.addic7ed.com/original/68018/4')
.to_return File.new('spec/responses/redirection_loop.http')
expect{ @reset_episode.download_best_subtitle!('fr') }.to raise_error(Addic7ed::HTTPError)
end

it 'should create a new file on disk' do
file = double('file')
Kernel.should_receive(:open).with('The.Walking.Dead.S03E02.720p.HDTV.x264-EVOLVE.srt', 'w').and_yield(file)
file.should_receive(:<<)
@reset_episode.download_best_subtitle!('fr')
end

end

Expand Down

0 comments on commit 0646ea9

Please sign in to comment.