Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

support `config.before(:each, :unless => condition)` #612

Closed
dchelimsky opened this Issue · 6 comments

5 participants

@dchelimsky
Owner

Background: #611

rspec-core supports the following means of attaching before hooks to selected examples and groups:

RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
  c.before :each, :do_something_before do
    # ...
  end
end

describe Thing, :do_something_before do # all examples in this group get the before hook
  # ...
end

describe OtherThing do
  it "does one thing", :do_something_before do # gets the before hook
    # ...
  end

  it "does another thing" do # does not get the before hook
    # ...
  end
end

It should also support its inverse: attach a before block to everything that is not tagged:

RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
  c.before :each, :unless => :do_not_do_something_before do
    # ...
  end
end

describe OtherThing do
  it "does one thing", :do_not_do_something_before do # does not get the before hook
    # ...
  end

  it "does another thing" do # gets the before hook
    # ...
  end
end
@myronmarston

I actually think that c.before :each, :unless => :do_not_do_something_before is a confusing API, because we already have built-in :if and :unless metadata options, and they work based on the truthiness of the supplied value. This, in contrast, is providing a tag, and conditionally filtering example groups based on that. It feels quite different and confusing, IMO.

@samphippen
Collaborator

That pull request was a typo. I have updated it to reference the correct issue #621

@JonRowe
Owner

I also think this would be a confusing api, and potentially extra unnecessary complexity.

@myronmarston

Closing.

@ashrestha91

Is there a way to acheive this kind of functionality in rspec? For example, if you want a before config that logins on all controller specs except the application controller spec, or ones that meet some condition?

@myronmarston
Owner

For 2.14/2.99:

RSpec.configure do |c|
  c.before(:each) do
    unless example.metadata[:blah]
      # do something
    end
  end
end

For 2.99/3.0:

RSpec.configure do |c|
  c.before(:each) do |ex|
    unless ex.metadata[:blah]
      # do something
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.