|
| 1 | +require 'rails_helper' |
| 2 | +require 'rake' |
| 3 | +require 'yaml' |
| 4 | + |
| 5 | +RSpec.describe 'news Rakeタスク', type: :task do |
| 6 | + before { Rails.application.load_tasks } |
| 7 | + before { allow(Rails.env).to receive(:test?).and_return(true) } |
| 8 | + |
| 9 | + # テスト用に tmp/news.yml を使う |
| 10 | + let(:yaml_path) { Rails.root.join('tmp', 'news.yml') } |
| 11 | + let(:fetch_task) { Rake::Task['news:fetch'] } |
| 12 | + let(:import_task) { Rake::Task['news:import_from_yaml'] } |
| 13 | + let(:yaml_content) { YAML.safe_load(File.read(yaml_path), permitted_classes: [Time]) } |
| 14 | + |
| 15 | + around do |example| |
| 16 | + # テスト前後に一度だけ tmp/news.yml をクリア |
| 17 | + File.delete(yaml_path) if File.exist?(yaml_path) |
| 18 | + example.run |
| 19 | + File.delete(yaml_path) if File.exist?(yaml_path) |
| 20 | + end |
| 21 | + |
| 22 | + describe 'news:fetch タスク' do |
| 23 | + before do |
| 24 | + ENV['NEWS_YAML_PATH'] = yaml_path.to_s |
| 25 | + ENV['NEWS_RSS_PATH'] = Rails.root.join('spec', 'fixtures', 'sample_news.rss').to_s |
| 26 | + fetch_task.reenable |
| 27 | + end |
| 28 | + |
| 29 | + after do |
| 30 | + ENV.delete('NEWS_YAML_PATH') |
| 31 | + ENV.delete('NEWS_RSS_PATH') |
| 32 | + end |
| 33 | + |
| 34 | + it 'サンプルRSSからニュースを取得し YAML に書き込む' do |
| 35 | + expect { fetch_task.invoke }.not_to raise_error |
| 36 | + expect(File.exist?(yaml_path)).to be true |
| 37 | + expect(yaml_content['news']).to be_an(Array) |
| 38 | + expect(yaml_content['news'].size).to eq(3) |
| 39 | + end |
| 40 | + |
| 41 | + it 'ID が 1 から連番で付与される' do |
| 42 | + fetch_task.invoke |
| 43 | + ids = yaml_content['news'].map { |item| item['id'] } |
| 44 | + expect(ids).to eq([1, 2, 3]) |
| 45 | + end |
| 46 | + |
| 47 | + it '公開日時で降順ソートされる' do |
| 48 | + fetch_task.invoke |
| 49 | + dates = yaml_content['news'].map { |item| Time.parse(item['published_at']) } |
| 50 | + expect(dates).to eq(dates.sort.reverse) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe 'news:import_from_yaml タスク' do |
| 55 | + let(:news_data) do |
| 56 | + { |
| 57 | + 'news' => [ |
| 58 | + { 'id' => 1, 'url' => 'https://example.com/test1', 'title' => 'テスト記事1', 'published_at' => '2025-01-01T10:00:00Z' }, |
| 59 | + { 'id' => 2, 'url' => 'https://example.com/test2', 'title' => 'テスト記事2', 'published_at' => '2025-01-02T10:00:00Z' } |
| 60 | + ] |
| 61 | + } |
| 62 | + end |
| 63 | + |
| 64 | + before do |
| 65 | + ENV['NEWS_YAML_PATH'] = yaml_path.to_s |
| 66 | + File.write(yaml_path, news_data.to_yaml) |
| 67 | + import_task.reenable |
| 68 | + end |
| 69 | + |
| 70 | + after do |
| 71 | + ENV.delete('NEWS_YAML_PATH') |
| 72 | + end |
| 73 | + |
| 74 | + it 'YAML ファイルから News レコードを新規作成する' do |
| 75 | + expect { import_task.invoke }.to change(News, :count).by(2) |
| 76 | + expect(News.find_by(url: news_data['news'][0]['url']).title).to eq('テスト記事1') |
| 77 | + expect(News.find_by(url: news_data['news'][1]['url']).title).to eq('テスト記事2') |
| 78 | + end |
| 79 | + |
| 80 | + it '既存レコードがあれば属性を更新する' do |
| 81 | + create(:news, url: news_data['news'][0]['url'], title: '古いタイトル') |
| 82 | + expect { import_task.invoke }.to change(News, :count).by(1) |
| 83 | + expect(News.find_by(url: news_data['news'][0]['url']).title).to eq('テスト記事1') |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments