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

Commit

Permalink
Browse files Browse the repository at this point in the history
Make TestStubs able to return test specific canned responses
  • Loading branch information
edgenard committed May 13, 2018
1 parent 7f05d31 commit abbf9cb
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions refactoring_external_service/youtube_video_list_spec.rb
Expand Up @@ -6,29 +6,42 @@
class VideoServiceTest < MiniTest::Test

class VideoRepoStub
attr_reader :canned_response

This comment has been minimized.

Copy link
@medwards1771

medwards1771 May 15, 2018

Contributor

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)
Expand Down

0 comments on commit abbf9cb

Please sign in to comment.