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

Commit

Permalink
Remove unnecessary method, and change FakeTest classes to Stubs becau…
Browse files Browse the repository at this point in the history
…se they return canned responses
  • Loading branch information
edgenard committed May 13, 2018
1 parent d6f75bf commit 7f05d31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
15 changes: 5 additions & 10 deletions refactoring_external_service/youtube_video_list.rb
Expand Up @@ -21,21 +21,22 @@ def video_stats(youtube_ids)
part: 'snippet, contentDetails, statistics', part: 'snippet, contentDetails, statistics',
} }
} }

response = JSON.parse(client.execute!(request).body) response = JSON.parse(client.execute!(request).body)
end end
end end


class VideoService class VideoService
attr_reader :video_repo attr_reader :video_repo, :video_client
def initialize(video_repo:) def initialize(video_repo:, video_client:)
@video_repo = video_repo @video_repo = video_repo
@video_client = video_client
end end


def video_list def video_list
@video_list = video_repo.videos @video_list = video_repo.videos
ids = @video_list.map{|v| v['youtubeID']} ids = @video_list.map{|v| v['youtubeID']}
response = get_youtube_stats_on_videos(ids) response = video_client.video_stats(ids)
ids.each do |id| ids.each do |id|
video = @video_list.find{|v| id == v['youtubeID']} video = @video_list.find{|v| id == v['youtubeID']}
youtube_record = response['items'].find{|v| id == v['id']} youtube_record = response['items'].find{|v| id == v['id']}
Expand All @@ -45,10 +46,4 @@ def video_list
end end
return JSON.dump(@video_list) return JSON.dump(@video_list)
end end

private

def get_youtube_stats_on_videos(youtube_ids)
YoutubeVideoClient.new.video_stats(youtube_ids)
end
end end
23 changes: 11 additions & 12 deletions refactoring_external_service/youtube_video_list_spec.rb
Expand Up @@ -5,14 +5,14 @@


class VideoServiceTest < MiniTest::Test class VideoServiceTest < MiniTest::Test


class VideoRepoMock class VideoRepoStub
def videos def videos
[{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}] [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
end end
end end


class YoutubeVideoClientMock class YoutubeVideoClientStub
def get_video_stats(ids=:NotGiven) def video_stats(ids=:NotGiven)
{'items' => {'items' =>
[ [
{ {
Expand All @@ -26,16 +26,15 @@ def get_video_stats(ids=:NotGiven)
end end


def test_video_list_returns_video_list def test_video_list_returns_video_list
video_service = VideoService.new(video_repo: VideoRepoMock.new) video_service = VideoService.new(video_repo: VideoRepoStub.new, video_client: YoutubeVideoClientStub.new)
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}] video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
youtube_response = YoutubeVideoClientMock.new.get_video_stats()
video_json = JSON.generate(video_array) video_json = JSON.generate(video_array)
video_service.stub(:get_youtube_stats_on_videos, youtube_response) do
result = JSON.parse(video_service.video_list) result = JSON.parse(video_service.video_list)
actual = JSON.parse(video_json) actual = JSON.parse(video_json)
assert_equal(result[0]['youtubeID'], result[0]['youtubeID']) assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
assert_equal(result[0]['views'], result[0]['views']) assert_equal(result[0]['views'], result[0]['views'])
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1) assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)
end
end end
end end

0 comments on commit 7f05d31

Please sign in to comment.