require 'rails_helper'
require 'rake'

RSpec.describe 'post:sync' do
  around do |example|
    original_application = Rake.application
    Rake.application = Rake::Application.new

    Rake::Task.define_task(:environment)
    load Rails.root.join('lib/tasks/sync_posts.rake')

    example.run
  ensure
    Rake.application = original_application
  end

  it 'runs Youtube::Sync' do
    sync = instance_double(Youtube::Sync)

    expect(Youtube::Sync).to receive(:new).once.and_return(sync)
    expect(sync).to receive(:sync!).once

    Rake::Task['post:sync'].invoke
  end
end
