Permalink
Browse files

Make TestStubs able to return test specific canned responses

  • Loading branch information...
edgenard committed May 13, 2018
1 parent 7f05d31 commit abbf9cbe64c9b934d4738b8fc09376a9f44c0fea
Showing with 27 additions and 14 deletions.
  1. +27 −14 refactoring_external_service/youtube_video_list_spec.rb
@@ -6,29 +6,42 @@
class VideoServiceTest < MiniTest::Test

class VideoRepoStub
attr_reader :canned_response

This comment has been minimized.

@medwards1771

medwards1771 May 15, 2018

Collaborator

I actually just noticed you have stubbed classes here for VideoRepo and YouTubeVideoClient. Can you tell us more about this usefulness of stubbing classes in your tests?

def initialize(canned_response)
@canned_response = canned_response
end
def videos
[{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
canned_response
end
end

class YoutubeVideoClientStub
def video_stats(ids=:NotGiven)
{'items' =>
[
{
'id' =>'blahblahblah',
'statistics' => {'viewCount' => '3'},
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
}
]
}
attr_reader :canned_response
def initialize(canned_response)
@canned_response = canned_response
end

def video_stats(ids=:NotGivenToStub)
canned_response
end
end

def test_video_list_returns_video_list
video_service = VideoService.new(video_repo: VideoRepoStub.new, video_client: YoutubeVideoClientStub.new)
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
video_json = JSON.generate(video_array)
video_repo_response = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
youtube_client_response = {'items' =>
[
{
'id' =>'blahblahblah',
'statistics' => {'viewCount' => '3'},
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
}
]
}
video_service = VideoService.new(
video_repo: VideoRepoStub.new(video_repo_response),
video_client: YoutubeVideoClientStub.new(youtube_client_response)
)
video_json = JSON.generate(video_repo_response)

result = JSON.parse(video_service.video_list)
actual = JSON.parse(video_json)

0 comments on commit abbf9cb

Please sign in to comment.