Skip to content

Commit d02b41d

Browse files
committed
Add test helper to stub no content item returned
It mimics stub_content_store_does_not_have_item in GdsApi adapters. GdsApi::HTTPErrorResponse params are based on: https://github.com/alphagov/gds-api-adapters/blob/4d4b2ab6969f333eb1cab94ff8e93d7ff8aeebf1/lib/gds_api/exceptions.rb#L25C1-L34C6
1 parent c11c1d5 commit d02b41d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ loader = GovukGraphql::ConditionalContentItemLoader.new(
8686

8787
The decision logic is also exposed via `can_load_from_graphql?`, allowing applications to make the routing decision themselves and implement custom fallback or error-handling behaviour if needed.
8888

89+
## Test helpers
90+
91+
There are also test helpers for stubbing responses in other apps. See [lib/test_helpers.rb](/lib/test_helpers.rb).
92+
8993
## Licence
9094

9195
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

lib/govuk_content_item_loader/test_helpers.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,28 @@ def stub_conditional_loader_returns_content_item(body, options = {})
3131

3232
response
3333
end
34+
35+
# Stubs the loader returns a error response
36+
# The following options can be passed in:
37+
#
38+
# :status the HTTP status code for the error. Defaults to 404
39+
# :message the error message. Defaults to "Not Found"
40+
# :error_details optional additional error details to attach to the exception
41+
# :http_body optional raw response body to attach to the exception
42+
def stub_conditional_loader_does_not_return_content_item(options = {})
43+
status = options.fetch(:status, 404)
44+
message = options.fetch(:message, "Not Found")
45+
error_details = options[:error_details]
46+
http_body = options[:http_body]
47+
48+
error = GdsApi::HTTPErrorResponse.new(status, message, error_details, http_body)
49+
50+
loader = instance_double(GovukConditionalContentItemLoader)
51+
52+
allow(GovukConditionalContentItemLoader).to receive(:new).with(request: anything)
53+
.and_return(loader)
54+
allow(loader).to receive(:load).and_raise(error)
55+
56+
error
57+
end
3458
end

0 commit comments

Comments
 (0)