← Back to Upcase

Can rspec tests be passed params?

(drew) #1

I want to acheive this:

describe “i have (10) figs” do |n|
expect(n).to eql(10)

or maybe something like:

| n |
| 5 |
| 3|

where it searches through a table or params to pass into the test. This is possible in cucumber, but i don’t see it mentioend in rspec. How about minitest? This was a great feature of pytest that i’m hoping i can find in rubyland.

(Joel Quenneville) #2

You can do this by nesting it blocks inside an each. it, describe, and other RSpec constructs are just Ruby and they can be combined with other Ruby methods.

Something like:

(1..10).each do |n|
  it "is 10" do
    expect(n). to eq 10

In a more real-life example, you might iterate over some params or an array.

Keep in mind that while this approach can solve a lot of duplication, it is a way to “meta-program” your tests and can make them obscure, hard to read, and hard to change.