Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit c3daffc

Browse files
author
Tim Mertens
committed
Add documentation for hook filters.
1 parent d8d0a34 commit c3daffc

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

features/hooks/filtering.feature

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,147 @@ Feature: filters
283283
"""
284284
When I run `rspec filter_example_hooks_with_symbol_spec.rb`
285285
Then the examples should all pass
286+
287+
Scenario: Filtering hooks using a hash
288+
Given a file named "filter_example_hooks_with_hash_spec.rb" with:
289+
"""ruby
290+
RSpec.configure do |config|
291+
config.before(:example, :foo => { :bar => :baz, :slow => true }) do
292+
invoked_hooks << :before_example_foo_bar
293+
end
294+
end
295+
296+
RSpec.describe "a filtered before :example hook" do
297+
let(:invoked_hooks) { [] }
298+
299+
describe "group without matching metadata" do
300+
it "does not run the hook" do
301+
expect(invoked_hooks).to be_empty
302+
end
303+
304+
it "does not run the hook for an example if only part of the filter matches", :foo => { :bar => :baz } do
305+
expect(invoked_hooks).to be_empty
306+
end
307+
308+
it "runs the hook for an example if the metadata contains all key value pairs from the filter", :foo => { :bar => :baz, :slow => true, :extra => :pair } do
309+
expect(invoked_hooks).to eq([:before_example_foo_bar])
310+
end
311+
end
312+
313+
describe "group with matching metadata", :foo => { :bar => :baz, :slow => true } do
314+
it "runs the hook" do
315+
expect(invoked_hooks).to eq([:before_example_foo_bar])
316+
end
317+
end
318+
end
319+
"""
320+
When I run `rspec filter_example_hooks_with_hash_spec.rb`
321+
Then the examples should all pass
322+
323+
Scenario: Filtering hooks using a Proc
324+
Given a file named "filter_example_hooks_with_proc_spec.rb" with:
325+
"""ruby
326+
RSpec.configure do |config|
327+
config.before(:example, :foo => Proc.new { |value| value.is_a?(String) } ) do
328+
invoked_hooks << :before_example_foo_bar
329+
end
330+
end
331+
332+
RSpec.describe "a filtered before :example hook" do
333+
let(:invoked_hooks) { [] }
334+
335+
describe "group without matching metadata" do
336+
it "does not run the hook" do
337+
expect(invoked_hooks).to be_empty
338+
end
339+
340+
it "does not run the hook if the proc returns false", :foo => :bar do
341+
expect(invoked_hooks).to be_empty
342+
end
343+
344+
it "runs the hook if the proc returns true", :foo => 'bar' do
345+
expect(invoked_hooks).to eq([:before_example_foo_bar])
346+
end
347+
end
348+
349+
describe "group with matching metadata", :foo => 'bar' do
350+
it "runs the hook" do
351+
expect(invoked_hooks).to eq([:before_example_foo_bar])
352+
end
353+
end
354+
end
355+
"""
356+
When I run `rspec filter_example_hooks_with_proc_spec.rb`
357+
Then the examples should all pass
358+
359+
Scenario: Filtering hooks using a regular expression
360+
Given a file named "filter_example_hooks_with_regexp_spec.rb" with:
361+
"""ruby
362+
RSpec.configure do |config|
363+
config.before(:example, :foo => /bar/ ) do
364+
invoked_hooks << :before_example_foo_bar
365+
end
366+
end
367+
368+
RSpec.describe "a filtered before :example hook" do
369+
let(:invoked_hooks) { [] }
370+
371+
describe "group without matching metadata" do
372+
it "does not run the hook" do
373+
expect(invoked_hooks).to be_empty
374+
end
375+
376+
it "does not run the hook if the value does not match", :foo => 'baz' do
377+
expect(invoked_hooks).to be_empty
378+
end
379+
380+
it "runs the hook if the value matches", :foo => 'bar' do
381+
expect(invoked_hooks).to eq([:before_example_foo_bar])
382+
end
383+
end
384+
385+
describe "group with matching metadata", :foo => 'bar' do
386+
it "runs the hook" do
387+
expect(invoked_hooks).to eq([:before_example_foo_bar])
388+
end
389+
end
390+
end
391+
"""
392+
When I run `rspec filter_example_hooks_with_regexp_spec.rb`
393+
Then the examples should all pass
394+
395+
Scenario: Filtering hooks using string comparison
396+
Given a file named "filter_example_hooks_with_strcmp_spec.rb" with:
397+
"""ruby
398+
RSpec.configure do |config|
399+
config.before(:example, :foo => :bar ) do
400+
invoked_hooks << :before_example_foo_bar
401+
end
402+
end
403+
404+
RSpec.describe "a filtered before :example hook" do
405+
let(:invoked_hooks) { [] }
406+
407+
describe "group without matching metadata" do
408+
it "does not run the hook" do
409+
expect(invoked_hooks).to be_empty
410+
end
411+
412+
it "does not run the hook if the coerced values do not match", :foo => 'baz' do
413+
expect(invoked_hooks).to be_empty
414+
end
415+
416+
it "does not run the hook if the coerced values match", :foo => 'bar' do
417+
expect(invoked_hooks).to eq([:before_example_foo_bar])
418+
end
419+
end
420+
421+
describe "group with matching metadata", :foo => 'bar' do
422+
it "runs the hook" do
423+
expect(invoked_hooks).to eq([:before_example_foo_bar])
424+
end
425+
end
426+
end
427+
"""
428+
When I run `rspec filter_example_hooks_with_strcmp_spec.rb`
429+
Then the examples should all pass

0 commit comments

Comments
 (0)