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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:46:48+00:00 2026-05-27T17:46:48+00:00

I have a Rails app using AuthLogic and have gotten it to the point

  • 0

I have a Rails app using AuthLogic and have gotten it to the point of using test doubles to step in for the current_user. There are a limited set of states for the current_user that are used repeatedly in many controller specs.

Continuing from this example, imagine controllers with similar double setup in each context. You can see that’s a lot of repeated code. What’s the preferred way for applying some DRY for this?

controller_1_spec.rb

require 'spec_helper'

describe Controller1 do

  describe "GET 'index'" do

    context "when user has state A" do
      before(:each) do
        user = double("User")
        user.stub(:state).and_return("A")
        user_session = double("UserSession")
        user_session.stub(:user).and_return(user)
        UserSession.stub(:find).and_return(user_session)
      end

      it "should test something for users with State A" do
        get :index
      end

      it "should test something else for users with State A" do
        get :index
      end
    end

    context "when user has state B" do
      before(:each) do
        user = double("User")
        user.stub(:state).and_return("B")
        user_session = double("UserSession")
        user_session.stub(:user).and_return(user)
        UserSession.stub(:find).and_return(user_session)
      end

      it "should test something for users with State B" do
        get :index
      end

      it "should test something else for users with State B" do
        get :index
      end
    end
  end
end

Is the best way just to add helper methods to spec_helper or is there something more best practice like?

  • 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-27T17:46:49+00:00Added an answer on May 27, 2026 at 5:46 pm

    Move the setup code to helper methods defined in spec_helper.rb or in a spec/support/{something}.rb file.

    def userInAStateWithSession
      user = double("User")
      user.stub(:state).and_return("A")
      user_session = double("UserSession")
      user_session.stub(:user).and_return(user)
      UserSession.stub(:find).and_return(user_session)
      return user
    end
    

    –

    describe Controller1 do
    
      describe "GET 'index'" do
    
        context "when user has state A" do
          before(:each) do
            user = userInAStateWithSession
          end
    
          ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some trouble with using authlogic in my rails app, so I began
I'm building an app in Rails 3 using Authlogic for authentication. I have a
I have already implemented some AJAX pagination in my Rails app by using the
I have a RESTful resource in my Rails app called Photo. I'm using Paperclip
I've been trying to build my first rails app and have gotten stuck on
I have a rails app with the config/environment.rb line config.gem 'authlogic', :version => '2.1.2'
I have a jruby/rails app using: jruby 1.4.0 Rails 2.3.5 ActiveMQ 5.3.0 Mule ESB
I am using mongomapper throughotu my rails 3 app but am using authlogic and
I'm trying to get Facebook logins working on my app using Rails, Authlogic, and
I have a rails app using delayed job, it works fine in development. However,

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.