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

Commit abbf9cb

Browse files
committed
Make TestStubs able to return test specific canned responses
1 parent 7f05d31 commit abbf9cb

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

refactoring_external_service/youtube_video_list_spec.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@
66
class VideoServiceTest < MiniTest::Test
77

88
class VideoRepoStub
9+
attr_reader :canned_response
10+
def initialize(canned_response)
11+
@canned_response = canned_response
12+
end
913
def videos
10-
[{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
14+
canned_response
1115
end
1216
end
1317

1418
class YoutubeVideoClientStub
15-
def video_stats(ids=:NotGiven)
16-
{'items' =>
17-
[
18-
{
19-
'id' =>'blahblahblah',
20-
'statistics' => {'viewCount' => '3'},
21-
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
22-
}
23-
]
24-
}
19+
attr_reader :canned_response
20+
def initialize(canned_response)
21+
@canned_response = canned_response
22+
end
23+
24+
def video_stats(ids=:NotGivenToStub)
25+
canned_response
2526
end
2627
end
2728

2829
def test_video_list_returns_video_list
29-
video_service = VideoService.new(video_repo: VideoRepoStub.new, video_client: YoutubeVideoClientStub.new)
30-
video_array = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
31-
video_json = JSON.generate(video_array)
30+
video_repo_response = [{'youtubeID' => 'blahblahblah', 'views' => 3, 'monthlyViews' => 3}]
31+
youtube_client_response = {'items' =>
32+
[
33+
{
34+
'id' =>'blahblahblah',
35+
'statistics' => {'viewCount' => '3'},
36+
'snippet' => {'publishedAt' => (Date.today - 30).to_s }
37+
}
38+
]
39+
}
40+
video_service = VideoService.new(
41+
video_repo: VideoRepoStub.new(video_repo_response),
42+
video_client: YoutubeVideoClientStub.new(youtube_client_response)
43+
)
44+
video_json = JSON.generate(video_repo_response)
3245

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

0 commit comments

Comments
 (0)