tomykaira / rspec-parameterized

RSpec::Parameterized supports simple parameterized test syntax in rspec.
MIT License
417 stars 28 forks source link

Customizable description #41

Closed aliaksandr-martsinovich closed 7 years ago

aliaksandr-martsinovich commented 7 years ago

This PR allows users to specify custom descriptions using magic argument :case_name

describe "Custom names for regular syntax" do
  where(:case_name, :a, :b, :answer) do
    [
        ["positive integers",  6,  2,  8],
        ["negative integers", -1, -2, -3],
        [   "mixed integers", -5,  3, -2],
    ]
  end

  with_them do
    it "should do additions" do
      expect(a + b).to eq answer
  end
end

# Output:
# Custom test case name
#   positive integers
#     should do additions
#   negative integers
#     should do additions
#   mixed integers
#     should do additions

For hash syntax you can specify a lambda that would be fed with other arguments:

describe "Custom naming for hash syntax" do
  where(case_names: ->(a, b, c){"#{a} + #{b} + #{c}"}, a: [1, 3], b: [5, 7, 9], c: [2, 4])

  with_them do
    it "sum is even" do
      expect(a + b + c).to be_even
    end
  end
end
# Output:
# when hash arguments
#   1 + 5 + 2
#     sum is even
#   1 + 5 + 4
#     sum is even
#   1 + 7 + 2
#     sum is even
#   ...

Maybe not the most elegant solution, but I've been using it for a while and it's better then nothing.

sue445 commented 7 years ago

@aliaksandr-martsinovich LGTM

@joker1007 How do you think?

joker1007 commented 7 years ago

LGTM too. Thanks !!