Eli Lyonhart
Eli Lyonhart

Reputation: 11

Conditionally applying tags in RSpec loop

I have a test file which begins with a .for_each loop. I want to apply a tag to one but not the other.

I could copy/paste the whole file, but that is very inelegant.

The test begins thusly:

[ "item1_name", "item2_name" ].each do |item|
  describe "Test Run on #{ item }" do

Is it possible? The below obviously wont work, but is descriptive of what I'm hoping for:

e.g.:

[ "item1_name", "item2_name" ].each do |item|
  if item == 'item1_name'
    describe "Test Run on #{ item }", :tag do
  else
    describe "Test Run on #{ item }", do
  end

  before :all do
    ...
  end

  ...
  expectations
  ...
end

Upvotes: 0

Views: 46

Answers (1)

Nate
Nate

Reputation: 2404

You could do the following, to conditionally add arguments to describe:

tags_for_items = {
  'item1_name' => [:tag],
  'item2_name' => [],
}

tags_for_items.each do |item, tags|

  describe "Test Run on #{item}", *tags do

    before :all do
      ...
    end

    ...
    expectations
    ...

  end

end

Upvotes: 1

Related Questions