Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 7f05d31

Browse files
committed
Remove unnecessary method, and change FakeTest classes to Stubs because they return canned responses
1 parent d6f75bf commit 7f05d31

2 files changed

Lines changed: 16 additions & 22 deletions

File tree

refactoring_external_service/youtube_video_list.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ def video_stats(youtube_ids)
2121
part: 'snippet, contentDetails, statistics',
2222
}
2323
}
24-
24+
2525
response = JSON.parse(client.execute!(request).body)
2626
end
2727
end
2828

2929
class VideoService
30-
attr_reader :video_repo
31-
def initialize(video_repo:)
30+
attr_reader :video_repo, :video_client
31+
def initialize(video_repo:, video_client:)
3232
@video_repo = video_repo
33+
@video_client = video_client
3334
end
3435

3536
def video_list
3637
@video_list = video_repo.videos
3738
ids = @video_list.map{|v| v['youtubeID']}
38-
response = get_youtube_stats_on_videos(ids)
39+
response = video_client.video_stats(ids)
3940
ids.each do |id|
4041
video = @video_list.find{|v| id == v['youtubeID']}
4142
youtube_record = response['items'].find{|v| id == v['id']}
@@ -45,10 +46,4 @@ def video_list
4546
end
4647
return JSON.dump(@video_list)
4748
end
48-
49-
private
50-
51-
def get_youtube_stats_on_videos(youtube_ids)
52-
YoutubeVideoClient.new.video_stats(youtube_ids)
53-
end
5449
end

refactoring_external_service/youtube_video_list_spec.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
class VideoServiceTest < MiniTest::Test
77

8-
class VideoRepoMock
8+
class VideoRepoStub
99
def videos
1010
[{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
1111
end
1212
end
1313

14-
class YoutubeVideoClientMock
15-
def get_video_stats(ids=:NotGiven)
14+
class YoutubeVideoClientStub
15+
def video_stats(ids=:NotGiven)
1616
{'items' =>
1717
[
1818
{
@@ -26,16 +26,15 @@ def get_video_stats(ids=:NotGiven)
2626
end
2727

2828
def test_video_list_returns_video_list
29-
video_service = VideoService.new(video_repo: VideoRepoMock.new)
29+
video_service = VideoService.new(video_repo: VideoRepoStub.new, video_client: YoutubeVideoClientStub.new)
3030
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
31-
youtube_response = YoutubeVideoClientMock.new.get_video_stats()
3231
video_json = JSON.generate(video_array)
33-
video_service.stub(:get_youtube_stats_on_videos, youtube_response) do
34-
result = JSON.parse(video_service.video_list)
35-
actual = JSON.parse(video_json)
36-
assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
37-
assert_equal(result[0]['views'], result[0]['views'])
38-
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)
39-
end
32+
33+
result = JSON.parse(video_service.video_list)
34+
actual = JSON.parse(video_json)
35+
assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
36+
assert_equal(result[0]['views'], result[0]['views'])
37+
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)
38+
4039
end
4140
end

0 commit comments

Comments
 (0)