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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:06:49+00:00 2026-05-12T05:06:49+00:00

One trick I have found very handy in rails application programming is that class_eval

  • 0

One trick I have found very handy in rails application programming is that class_eval can be used to create methods on the fly. I’m starting to get into testing now and I wonder if a similar idea could be used to generate tests.

For example, I have a before_filter to require a user to be logged in for all of the actions in a controller. I would like to write tests which ensure that the before_filter is being applied for all actions. Instead of writing out each test individually I would like to instead generate all of these test automatically.

Is this type of testing advisable, or should I just stick to writing the tests out individually? If it is, how would one go about doing so?

EDIT: This might look something like:

actions = {:index => :get,:show => :get,:edit => :get,:update => :put}
actions.each_pair do |action,type|
  class_eval(%Q{def test_user_required_for_#{action}
      set_active_user users(:one)
      #{type} :#{action}
      assert flash[:error]
      assert_redirected_to :action => :index
    end
  })
end

Now that people have verified this could be useful, where would I put a block of code such as this so that it will get executed once and only once to create these tests?

  • 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-12T05:06:49+00:00Added an answer on May 12, 2026 at 5:06 am

    The DRY principle applies to test code just as much as it applies to application code.

    Having one method to generate all of those tests should make it easier to verify that the test is correct in the first place.

    To answer the comment (note: I haven’t written Rails test code in a while, so it probably isn’t 100% correct). Everything between %| | is one big string:

    MyControllerTest
    
      [:index, :show, :new, :create, :edit, :update, :destroy].each do |action|
          class_eval do %|
            test "#{action} requires before filter" do
              #test #{action} code here
            end
          |
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a simple C# Winforms application that loads MS-Word 2007 documents via
I have a simple rails application with a few controller and some rake tasks.
I have found an API that'll make working with CANVAS a lot easier. It
I just found out about a very interesting Java trick: void method1(Integer... a){ }
I'm having a very odd problem that I can't seem to track down. Any
So i have some bodies(which i can create more and more at runtime), and
This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
I have a table view with numerous cells. When I press one, a tick
That one is a tricky one I believe. I would like to merge two
confused - given a track uri, I can get one album but possibly more

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.