Skip to content

Commit

Permalink
Move stub_request_for_schema to a helper
Browse files Browse the repository at this point in the history
We were getting flaky tests when using `stub_request_for_schema` in two
places because the helper function was defined in two files, but the
implementation was slightly different. This unifies the behaviour,
returning the stub schema in both places, whilst also moving the helper
code to a module, which we include when we need it.
  • Loading branch information
pezholio committed Oct 9, 2024
1 parent 1370f9c commit 4815e73
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ContentObjectStore::ContentBlock::EditionsTest < ActionDispatch::Integrati
include Capybara::DSL
extend Minitest::Spec::DSL
include ContentObjectStore::Engine.routes.url_helpers
include ContentObjectStore::IntegrationTestHelpers

before do
login_as_admin
Expand Down Expand Up @@ -185,12 +186,6 @@ class ContentObjectStore::ContentBlock::EditionsTest < ActionDispatch::Integrati
end
end

def stub_request_for_schema(block_type)
schema = stub(id: "content_block_type", fields: %w[foo bar], name: "schema", body: {}, block_type:)
ContentObjectStore::ContentBlock::Schema.stubs(:find_by_block_type).with(block_type).returns(schema)
schema
end

def renders_errors(&block)
content_block_edition = build(:content_block_edition, :email_address)
ContentObjectStore::CreateEditionService.any_instance.expects(:call).raises(ActiveRecord::RecordInvalid, content_block_edition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ContentObjectStore::ContentBlock::WorkflowTest < ActionDispatch::Integrati
extend Minitest::Spec::DSL
include SidekiqTestHelpers
include ContentObjectStore::Engine.routes.url_helpers
include ContentObjectStore::IntegrationTestHelpers

let(:details) do
{
Expand Down Expand Up @@ -176,11 +177,6 @@ def assert_edition_is_published(&block)
end
end

def stub_request_for_schema(block_type)
schema = stub(id: "content_block_type", fields: %w[foo bar], name: "schema", body: {}, block_type:)
ContentObjectStore::ContentBlock::Schema.stubs(:find_by_block_type).with(block_type).returns(schema)
end

def update_params(edition_id:, organisation_id:)
{
id: edition_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ContentObjectStore::IntegrationTestHelpers
def stub_request_for_schema(block_type)
schema = stub(id: "content_block_type", fields: %w[foo bar], name: "schema", body: {}, block_type:)
ContentObjectStore::ContentBlock::Schema.stubs(:find_by_block_type).with(block_type).returns(schema)
schema
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
end

Dir[Rails.root.join("test/support/*.rb")].sort.each { |f| require f }
Dir[Rails.root.join("lib/engines/**/test/support/*.rb")].sort.each { |f| require f }

Whitehall::Application.load_tasks if Rake::Task.tasks.empty?

Expand Down

0 comments on commit 4815e73

Please sign in to comment.