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

Closed
dchelimsky opened this Issue May 3, 2012 · 6 comments

Comments

Projects
None yet
5 participants
@dchelimsky
Member

dchelimsky commented May 3, 2012

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

This comment has been minimized.

Show comment Hide comment
@myronmarston

myronmarston Jun 15, 2012

Member

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.

Member

myronmarston commented Jun 15, 2012

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

This comment has been minimized.

Show comment Hide comment
@samphippen

samphippen Oct 7, 2012

Member

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

Member

samphippen commented Oct 7, 2012

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

@JonRowe

This comment has been minimized.

Show comment Hide comment
@JonRowe

JonRowe Mar 23, 2013

Member

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

Member

JonRowe commented Mar 23, 2013

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

@myronmarston

This comment has been minimized.

Show comment Hide comment
@myronmarston

myronmarston Mar 23, 2013

Member

Closing.

Member

myronmarston commented Mar 23, 2013

Closing.

@ashrestha91

This comment has been minimized.

Show comment Hide comment
@ashrestha91

ashrestha91 Nov 6, 2013

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?

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

This comment has been minimized.

Show comment Hide comment
@myronmarston

myronmarston Nov 6, 2013

Member

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
Member

myronmarston commented Nov 6, 2013

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