Skip to content

Commit 14936ba

Browse files
committed
Added specs for the new rake tasks and routine
1 parent 605153a commit 14936ba

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

lib/tasks/log_to_stdout.rake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# http://jerryclinesmith.me/blog/2014/01/16/logging-from-rake-tasks/
2+
desc 'Include this task as another task\'s dependency to cause Rails.logger to also print to STDOUT'
3+
task :log_to_stdout, [ :log_level ] => :environment do |tt, args|
4+
# Do nothing in the test environment, since we don't want stdout there
5+
next if Rails.env.test?
6+
7+
# Clone the main Rails logger to dissociate it from the other module loggers
8+
# so we don't receive database, background job and mailer logs
9+
Rails.logger = Rails.logger.clone
10+
11+
stdout_logger = ActiveSupport::Logger.new(STDOUT)
12+
13+
# By default, use a log level of at least 1 so we don't receive debug messages
14+
stdout_logger.level = args.fetch(:log_level) do
15+
ENV.fetch('LOG_LEVEL') { [ Rails.logger.level, 1 ].max }
16+
end
17+
stdout_logger.formatter = Rails.logger.formatter
18+
19+
Rails.logger.extend(ActiveSupport::Logger.broadcast(stdout_logger))
20+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'rails_helper'
2+
require 'rake'
3+
4+
RSpec.describe 'cron:day', type: :rake do
5+
before :all do
6+
Rake.application.rake_require 'tasks/cron/day'
7+
Rake::Task.define_task :log_to_stdout
8+
end
9+
10+
before { Rake::Task['cron:day'].reenable }
11+
12+
it 'calls the UpdateSlugs lev routine' do
13+
expect(UpdateSlugs).to receive(:call)
14+
15+
Rake.application.invoke_task 'cron:day'
16+
end
17+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'rails_helper'
2+
require 'rake'
3+
4+
RSpec.describe 'cron:minute', type: :rake do
5+
before :all do
6+
Rake.application.rake_require 'tasks/cron/minute'
7+
Rake::Task.define_task :log_to_stdout
8+
end
9+
10+
before { Rake::Task['cron:minute'].reenable }
11+
12+
let!(:task) { instance_double(Rake::Task) }
13+
14+
it 'calls the openstax:accounts:sync rake task' do
15+
expect(Rake::Task).to receive(:[]).with('openstax:accounts:sync:accounts').and_return(task)
16+
expect(task).to receive(:invoke)
17+
18+
Rake.application.invoke_task 'cron:minute'
19+
end
20+
end

spec/routines/update_slugs_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe UpdateSlugs, type: :routine do
4+
let!(:exercise) { FactoryBot.create :exercise }
5+
let!(:updated_exercise) { FactoryBot.create :exercise }
6+
7+
before do
8+
allow_any_instance_of(Exercise).to(
9+
receive(:set_slug_tags!) { |exercise| exercise.id == updated_exercise.id }
10+
)
11+
end
12+
13+
it 'calls set_slug_tags! on all exercises and sets updated_at for updated exercises' do
14+
expect { described_class.call }.to change { updated_exercise.reload.updated_at }
15+
.and not_change { exercise.reload.updated_at }
16+
end
17+
end

0 commit comments

Comments
 (0)