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 492973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:10:02+00:00 2026-05-13T02:10:02+00:00

I know how to run functional/integration tests in Rails, this question is about best

  • 0

I know how to run functional/integration tests in Rails, this question is about best practices. Let’s say authorization is performed using four distinct user roles:

  • basic
  • editor
  • admin
  • super

This means that for each action there are up to five different behaviors possible (4 roles + unauthenticated/anonymous). One approach I’ve taken is to test every role on every action, for example:

  • test_edit_by_anonymous_user
  • test_edit_by_basic_user
  • test_edit_by_editor_user
  • test_edit_by_admin_user
  • test_edit_by_super_user

But this obviously leads to a lot of tests (every controller action on the site really needs to be tested five times). The opposite approach would be to test the authorization mechanism in isolation and then authenticate as super before testing every action (on setup), and only test one version of each page.

I’ve tried several approaches with varying degrees of specificity but haven’t been completely satisfied with anything. I feel more comfortable when I’m testing more cases, but the amount of test code and difficulty of abstraction has been a turn-off. Does anyone have an approach to this problem that they’re satisfied with?

  • 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-13T02:10:02+00:00Added an answer on May 13, 2026 at 2:10 am

    It really depends on how you have setup your code for checking the authorization and how you test for it in actions. I can tell you what we do as an example. We have roles like you do, and some pages that require login, some that require a role, and some that have different output based on role. We test each type a little differently.

    First, we test authorization and login separately.

    Also, we created filters for actions that require the user has logged in, and then others for requiring a certain role. For example check_admin, check_account_owner, etc. We can then test that those filters work on their own.

    We then add checks in the controller tests that the correct filters are being called. We use shoulda and wrote some easy extensions so we can add checks like:

    should_filter_before_with :check_admin, :new
    

    That way we are testing what needs to be tested and no more.

    Now, for more complex actions that do different logic depending on role, we do test those actions for each role that contains special logic. We don’t write tests for roles on that action that will be filtered, or are supersets of other roles. For example if the action adds more fields to a form if you are an admin, we test non-admins and admin. We don’t test admin and super admin since our code for role checking understands that super-admins are admins.

    Also, for templates that contain logic to only display certain items for certain roles, we try and move that code into helpers, or if common like an admin toolbar, into partials. Then we can test those on their own and not on every action that includes them.

    To sum up, test only what you need to for a given action. Just like you wouldn’t test Rails internals in your Unit tests, if you write common code for your role checks and test that, you don’t need to test it again on every action.

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

Sidebar

Related Questions

I know about the basic concept of virtual function and run-time call. But i
OK - I know I can run my java process like this : java
I would like to know how to run a file like this: Search-Mailbox -Identity
This question is a bit vague but I'd like to know if there's a
Let's say I have this blob of code that's made to be one long-running
When I run a post in my Rails functional test setup do post :create,
In jquery when I run: $('li').each(some-function(i){}); The IDE seems to know that some-function is
You know how Temple Run sometimes has alerts when you open the App that
I know how to run a single target in ANT, but it also checks
I know that I can run an external Javascript file from within HTML with

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.