Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6737859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:17:53+00:00 2026-05-26T11:17:53+00:00

Is there a way to group tests conditionally with rspec? By which I mean,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:17:53+00:00Added an answer on May 26, 2026 at 11:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to group tests in JUnit, so that I can run
Is there a way to set up a user group that has permissions to
Is there any way to group the models in django admin interface? I currently
Is there a way to group a bunch of DLL's and still use them
SQL Experts, Is there an efficient way to group runs of data together using
Is there a way to get the name of a captured group in C#?
Is there a way to include a LIKE expression in a GROUP BY query?
I have group of stored procedures with names like 'somename_%'. Are there any way
My Facebook group is about my iPhone software. Is there a way to have
Is there an easy way to rename a group of files already contained in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.