Skip to content

Commit

Permalink
Add #fits and #xits methods
Browse files Browse the repository at this point in the history
RSpec core library has a convention of prepending 'f' and 'x' to it's methods to support running certain blocks or tests with :focus or :skip tags. This commit aims to bring rspec-its inline with this convention by making #fits and #xits methods available to the spec writer.
  • Loading branch information
bazay committed Dec 2, 2020
1 parent ba51505 commit 36aa30d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
bundle
bin
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ Metadata arguments are supported.
its(:size, focus: true) { should eq(1) }
```

Alternatively, you can use the `f` and `x` prefix for `focus` and `skip` metadata, respectively.

```ruby
fits(:size) { should eq(1) }
xits(:not_implemented_method) { will_not raise_error }
```

## Contributing

1. Fork it
Expand Down
30 changes: 29 additions & 1 deletion lib/rspec/its.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,40 @@ def should_not(matcher=nil, message=nil)
end
end

# Calls the #its method adding the :focus tag to metadata to allowing
# to be run in focus.
#
# @example
#
# # This ...
# describe Array do
# fits(:size) { should eq(0) }
# end
def fits(attribute, *options, &block)
opts = (Array(options) << :focus).uniq
its(attribute, *opts, &block)
end

# Calls the #its method adding the :skip tag to metadata to allowing
# to be be skipped.
#
# @example
#
# # This ...
# describe Array do
# xits(:size) { should eq(0) }
# end
def xits(attribute, *options, &block)
opts = (Array(options) << :skip).uniq
its(attribute, *opts, &block)
end

end
end

RSpec.configure do |rspec|
rspec.extend RSpec::Its
rspec.backtrace_exclusion_patterns << %r(/lib/rspec/its)
rspec.backtrace_exclusion_patterns << %r{/lib/rspec/its/fits/xits}
end

RSpec::SharedContext.send(:include, RSpec::Its)
55 changes: 55 additions & 0 deletions spec/rspec/its_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module RSpec
its([]) { expect(described_class).to be Its }
end
end

context "with explicit subject" do
subject do
Class.new do
Expand Down Expand Up @@ -243,6 +244,7 @@ def false_if_first_time
end
end
end

context "with metadata" do
context "preserves access to metadata that doesn't end in hash" do
its([], :foo) do |example|
Expand Down Expand Up @@ -376,5 +378,58 @@ def terminator
end
end
end

describe "#fits" do
it 'calls #its with :focus metadata' do
stub = Class.new do
include RSpec::Its
end.new
allow(stub).to receive(:its)

expect(stub).to receive(:its).with('attribute', :focus).and_yield
stub.fits('attribute') {}
end

it "fits is focused within the example group" do
ex_it, ex_fits = nil

RSpec.describe do
extend RSpec::Its

ex_it = its(:name) { :its_example }
ex_fits = fits(:class) { :fits_example }
end

expect(ex_it.examples.first.metadata[:focus]).to be_falsey
expect(ex_fits.examples.first.metadata[:focus]).to be_truthy
end
end

describe "#xits" do
it 'calls #its with :skip metadata' do
stub = Class.new do
include RSpec::Its
end.new
allow(stub).to receive(:its)

expect(stub).to receive(:its).with('attribute', :skip).and_yield
stub.xits('attribute') {}
end

it "xits is skipped within the example group" do
ex_it, ex_xits = nil

RSpec.describe do
extend RSpec::Its

ex_it = its(:name) { :its_example }
ex_xits = xits(:class) { :xits_example }
end

expect(ex_it.examples.first.metadata[:skip]).to be_falsey
expect(ex_xits.examples.first.metadata[:skip]).to be_truthy
end
end

end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rspec/its'

Dir['./spec/support/**/*'].each {|f| require f}
Dir['./support/**/*.rb'].each {|f| require f}

class NullFormatter
private
Expand Down

0 comments on commit 36aa30d

Please sign in to comment.