Skip to content

Commit 5feae02

Browse files
committed
Fix flaky tests in podcasts_spec
1 parent d6df75c commit 5feae02

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

spec/lib/tasks/podcasts_spec.rb

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,37 @@
2626

2727
let(:task) { 'podcasts:upsert' }
2828

29-
# TODO: This test is flaky? It seems fails depending on Internet and should work offline.
30-
xit 'successfuly fetch from Anchor.fm RSS' do
31-
allow_any_instance_of(Podcast).to receive(:id).and_return(
32-
[
33-
{ 'id' => 123456001,
34-
'title' => '999 - podcast title',
35-
'description' => '説明 999',
36-
'content_size' => 124542711,
37-
'duration' => 5189815,
38-
'user_id' => 123456789,
39-
'permalink' => '999-title',
40-
'permalink_url' => 'https://anchor.fm/coderdojo-japan/999-title',
41-
'created_at' => '2099/01/23 01:00:00 +0000' }
42-
]
29+
it 'successfuly fetch from RSS (mock)' do
30+
# RSSのモック
31+
rss_content = <<~RSS
32+
<?xml version="1.0" encoding="UTF-8"?>
33+
<rss version="2.0">
34+
<channel>
35+
<item>
36+
<title>001 - 日本の CoderDojo の成り立ち</title>
37+
<description>jishihaの説明文</description>
38+
<link>https://anchor.fm/coderdojo-japan/episodes/001----CoderDojo-euhits</link>
39+
<pubDate>Sat, 25 Mar 2017 00:00:00 GMT</pubDate>
40+
<enclosure url="https://example.com/podcast.mp3" length="22887860" type="audio/mpeg"/>
41+
<itunes:duration>2857</itunes:duration>
42+
</item>
43+
</channel>
44+
</rss>
45+
RSS
46+
47+
# RSS::Parserのモック
48+
rss_parser = instance_double(RSS::Rss)
49+
rss_item = instance_double(RSS::Rss::Channel::Item)
50+
allow(rss_item).to receive(:title).and_return('001 - 日本の CoderDojo の成り立ち')
51+
allow(rss_item).to receive(:description).and_return('jishihaの説明文')
52+
allow(rss_item).to receive(:link).and_return('https://anchor.fm/coderdojo-japan/episodes/001----CoderDojo-euhits')
53+
allow(rss_item).to receive(:pubDate).and_return(Time.parse('2017-03-25'))
54+
allow(rss_item).to receive(:enclosure).and_return(
55+
instance_double(RSS::Rss::Channel::Item::Enclosure, url: 'https://example.com/podcast.mp3', length: 22887860)
4356
)
57+
allow(rss_item).to receive(:itunes_duration).and_return(double(content: '2857', to_s: '2857'))
58+
allow(rss_parser).to receive(:items).and_return([rss_item])
59+
allow(RSS::Parser).to receive(:parse).and_return(rss_parser)
4460

4561
# before
4662
expect(Podcast.count).to eq(1)
@@ -52,14 +68,14 @@
5268
# after
5369
expect(Podcast.count).not_to eq(1)
5470
new_records = Podcast.where.not(id: before_ids)
55-
expect(new_records.count).not_to eq(1)
56-
first_track = new_records.find(1)
71+
expect(new_records.count).to eq(1)
72+
first_track = new_records.first
5773

5874
expect(first_track.title).to eq('001 - 日本の CoderDojo の成り立ち')
5975
expect(first_track.description).to start_with('jishiha')
6076
expect(first_track.content_size).to eq(22887860)
61-
expect(first_track.duration).to eq('2857') # eq('00:47:37')
62-
expect(first_track.permalink).to eq('001----CoderDojo-euhits') # eq('999-title')
77+
expect(first_track.duration).to eq('2857')
78+
expect(first_track.permalink).to eq('001----CoderDojo-euhits')
6379
expect(first_track.permalink_url).to eq('https://anchor.fm/coderdojo-japan/episodes/001----CoderDojo-euhits')
6480
expect(first_track.published_date).to eq('2017-03-25'.to_date)
6581
end

0 commit comments

Comments
 (0)