Skip to content

Commit

Permalink
Merge pull request #907 from alphagov/refactor-schedule-testing
Browse files Browse the repository at this point in the history
Refactor how we test async jobs
  • Loading branch information
benthorner authored Mar 26, 2019
2 parents 93c7d0d + 774f4e7 commit 3a2c493
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 30 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ group :development do
end

group :test do
gem "rspec-sidekiq", "~> 3"
gem "simplecov", "~> 0.16"
end

Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@ GEM
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-sidekiq (3.0.3)
rspec-core (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.8.0)
rubocop (0.64.0)
jaro_winkler (~> 1.5.1)
Expand Down Expand Up @@ -477,7 +474,6 @@ DEPENDENCIES
rails (~> 5.2)
rinku (~> 2)
rspec-rails (~> 3)
rspec-sidekiq (~> 3)
simplecov (~> 0.16)
uglifier (~> 4)
webmock (~> 3)
Expand Down
4 changes: 2 additions & 2 deletions spec/features/workflow/schedule_publishing_api_down_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

RSpec.feature "Schedule a document when Publishing API is down" do
scenario do
given_there_is_an_edition_with_set_scheduled_publishing_datetime
given_there_is_an_edition_ready_to_schedule
and_the_publishing_api_is_down
when_i_visit_the_summary_page
and_i_try_to_schedule_the_edition
then_i_see_an_error_message
and_the_document_has_not_been_scheduled
end

def given_there_is_an_edition_with_set_scheduled_publishing_datetime
def given_there_is_an_edition_ready_to_schedule
@datetime = Time.current.tomorrow
@edition = create(:edition, scheduled_publishing_datetime: @datetime)
end
Expand Down
25 changes: 8 additions & 17 deletions spec/features/workflow/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

RSpec.feature "Schedule an edition" do
scenario do
given_there_is_an_edition_with_set_scheduled_publishing_datetime
given_there_is_an_edition_ready_to_schedule
when_i_visit_the_summary_page
and_i_click_on_schedule
and_i_select_the_content_has_been_reviewed_option
and_i_click_on_publish
then_i_see_a_confirmation_that_the_edition_has_been_scheduled

when_i_visit_the_summary_page
then_i_see_the_edition_has_been_scheduled
and_i_can_no_longer_see_a_schedule_action
and_i_can_no_longer_edit_the_content
end

def given_there_is_an_edition_with_set_scheduled_publishing_datetime
def given_there_is_an_edition_ready_to_schedule
@datetime = Time.current.tomorrow
@edition = create(:edition, scheduled_publishing_datetime: @datetime)
@request = stub_default_publishing_api_put_intent
Expand All @@ -37,25 +33,20 @@ def and_i_click_on_publish
click_on "Publish"
end

def then_i_see_a_confirmation_that_the_edition_has_been_scheduled
def then_i_see_the_edition_has_been_scheduled
expect(page).to have_content(I18n.t!("schedule.scheduled.title"))
expect(Sidekiq::Worker.jobs.count).to eq 1

govuk_header_args = an_instance_of(Hash) # args from govuk_sidekiq gem
expect(ScheduledPublishingWorker)
.to have_enqueued_sidekiq_job(@edition.id, govuk_header_args)
.at(@datetime)
job = Sidekiq::Worker.jobs.first
expect(job["args"].first).to eq @edition.id
expect(job["at"].to_i).to eq @datetime.to_i

assert_requested @request
end

def then_i_see_the_edition_has_been_scheduled
visit document_path(@edition.document)
expect(page).to have_content(I18n.t!("user_facing_states.scheduled.name"))
end

def and_i_can_no_longer_see_a_schedule_action
expect(page).not_to have_link("Schedule")
end

def and_i_can_no_longer_edit_the_content
expect(page).not_to have_link("Change Content")
end
Expand Down
11 changes: 5 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "gds_api/test_helpers/publishing_api"
require "gds_api/test_helpers/publishing_api_v2"
require "gds_api/test_helpers/asset_manager"
require "govuk_sidekiq/testing"

Dir[Rails.root.join("spec", "support", "**", "*.rb")].each { |f| require f }
SimpleCov.start
Expand All @@ -28,12 +29,6 @@
/the server responded with a status of 422/i,
]

Sidekiq::Logging.logger = nil

RSpec::Sidekiq.configure do |config|
config.warn_when_jobs_not_processed_by_sidekiq = false
end

RSpec.configure do |config|
config.expose_dsl_globally = false
config.infer_spec_type_from_file_location!
Expand All @@ -60,6 +55,10 @@
reset_authentication
end

config.before :each do
Sidekiq::Worker.clear_all
end

config.after :each, type: :feature, js: true do
Capybara::Chromedriver::Logger::TestHooks.after_example!
end
Expand Down

0 comments on commit 3a2c493

Please sign in to comment.