Skip to content

Commit 871bb40

Browse files
committed
Extract #with_options test helper
`#with_options` scopes configuration options to a given test block. For example: ```rb context "when configured to record errors" do it "fails when the body is missing a required property" do with_options(record_errors: true) do create_schema("foo_schema", "type" => "object", "required" => ["foo"]) expect(response_for({})).not_to match_response_schema("foo_schema") end end end end ``` * Configures `JsonMatchers` to `record_errors: true` for the scope of the test * yields the test block, configuring the underlying validator with the options * resets `JsonMatchers` to the original (likely default) options after the test has executed.
1 parent 1a5c39e commit 871bb40

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

spec/json_matchers/match_response_schema_spec.rb

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -142,49 +142,31 @@
142142

143143
context "when options are configured globally" do
144144
it "forwards them to the validator" do
145-
create_schema("foo_schema", {
146-
"type" => "object",
147-
"properties" => {
148-
"id" => { "type" => "number" },
149-
"title" => { "type" => "string" },
150-
},
151-
})
152-
153-
JsonMatchers.configure do |config|
154-
config.options[:strict] = true
155-
end
156-
157-
expect(response_for({ "id" => 1, "title" => "bar" })).
158-
to match_response_schema("foo_schema")
159-
expect(response_for({ "id" => 1 })).
160-
not_to match_response_schema("foo_schema")
161-
end
162-
163-
after do
164-
JsonMatchers.configure do |config|
165-
config.options.delete(:strict)
145+
with_options(strict: true) do
146+
create_schema("foo_schema", {
147+
"type" => "object",
148+
"properties" => {
149+
"id" => { "type" => "number" },
150+
"title" => { "type" => "string" },
151+
},
152+
})
153+
154+
expect(response_for({ "id" => 1, "title" => "bar" })).
155+
to match_response_schema("foo_schema")
156+
expect(response_for({ "id" => 1 })).
157+
not_to match_response_schema("foo_schema")
166158
end
167159
end
168160

169-
context "when options specify to record errors" do
170-
around do |example|
171-
JsonMatchers.configure do |config|
172-
config.options[:record_errors] = true
173-
end
174-
175-
example.run
176-
177-
JsonMatchers.configure do |config|
178-
config.options.delete(:record_errors)
179-
end
180-
end
181-
161+
context "when configured to record errors" do
182162
it "fails when the body is missing a required property" do
183-
create_schema("foo_schema",
184-
"type" => "object",
185-
"required" => ["foo"])
163+
with_options(record_errors: true) do
164+
create_schema("foo_schema",
165+
"type" => "object",
166+
"required" => ["foo"])
186167

187-
expect(response_for({})).not_to match_response_schema("foo_schema")
168+
expect(response_for({})).not_to match_response_schema("foo_schema")
169+
end
188170
end
189171
end
190172
end

spec/support/configuration.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module ConfigurationHelpers
2+
def with_options(options)
3+
original_options = JsonMatchers.configuration.options.dup
4+
5+
JsonMatchers.configure do |config|
6+
config.options.merge!(options)
7+
end
8+
9+
yield
10+
11+
JsonMatchers.configure do |config|
12+
config.options.clear
13+
config.options.merge!(original_options)
14+
end
15+
end
16+
end
17+
18+
RSpec.configure do |config|
19+
config.include ConfigurationHelpers
20+
end

0 commit comments

Comments
 (0)