Is there a way to group tests conditionally with rspec? By which I mean, is there a way to say “if this variable is some value, run this set of tests. If this variable is some other variable, run this other set of tests”?
Basic Example of where it would be needed (doesn’t actually work, obviously, but should show you what I want). Assume the user to be tested is defined elsewhere and the current user being tested is @user. Although you may have better alternatives to that, that’s fine.
before do
login_as_user(@user) #This logs them in and brings them to the homepage to be tested
page.visit("/homepage")
end
describe "Check the user homepage"
subject {page}
it {should have_content("Welcome, #{@user.name}!")}
if(@user.role=="admin")
it {should have_link("List Users"}
end
end
Keep in mind I have no control over the user being tested – I cannot create users on the fly, for example, and no given user is guaranteed to exist, and I don’t know offhand what combination of roles a given user will have. So I do need some way to say “run this test only if these conditions are met”, rather than a way to create situations where every test can be run.
Okay, apparently the issue was as simple as this: @user is an instance variable, which only exists when the tests are being executed. Dynamic generation of tests does not work with instance variables for that reason.
However, by declaring it as a local variable somewhere outside any test-style blocks (before, after, it or specify), you can have access to it for the conditional logic.
The solution was as simple as taking the @ sign off the front of the user.