Permalink
Browse files

Remove unnecessary method, and change FakeTest classes to Stubs becau…

…se they return canned responses
  • Loading branch information...
edgenard committed May 13, 2018
1 parent d6f75bf commit 7f05d312db498811d95ca0d70e6c7773922ef8cc
@@ -21,21 +21,22 @@ def video_stats(youtube_ids)
part: 'snippet, contentDetails, statistics',
}
}

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

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

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

private

def get_youtube_stats_on_videos(youtube_ids)
YoutubeVideoClient.new.video_stats(youtube_ids)
end
end
@@ -5,14 +5,14 @@

class VideoServiceTest < MiniTest::Test

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

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

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}]
youtube_response = YoutubeVideoClientMock.new.get_video_stats()
video_json = JSON.generate(video_array)
video_service.stub(:get_youtube_stats_on_videos, youtube_response) do
result = JSON.parse(video_service.video_list)
actual = JSON.parse(video_json)
assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
assert_equal(result[0]['views'], result[0]['views'])
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)
end

result = JSON.parse(video_service.video_list)
actual = JSON.parse(video_json)
assert_equal(result[0]['youtubeID'], result[0]['youtubeID'])
assert_equal(result[0]['views'], result[0]['views'])
assert_in_delta(result[0]['monthlyViews'], result[0]['monthlyViews'], 0.1)

end
end

0 comments on commit 7f05d31

Please sign in to comment.