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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:46:49+00:00 2026-05-16T22:46:49+00:00

Using Rails 3.0, I have a small bit of code that I seem to

  • 0

Using Rails 3.0, I have a small bit of code that I seem to be calling in all my Unit tests.

  1. Is there a common place to put shared unit test code that won’t be incorporated into both unit and functional tests? I’d rather ONLY incorporate it into the needed files, just the unit tests.

  2. I’m new to testing practices in general. Is it common in Rails to test validations (for example, test: validates_presence_of :name)? Most of my functions that test validations on a model are all basically the same (just swap in and out correct fixture and attribute name). Doesn’t seem very DRY. Looking for a way to refactor.

Thanks.

  • 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-16T22:46:49+00:00Added an answer on May 16, 2026 at 10:46 pm

    You can place the code in the test/test_helper.rb file.
    You should already find a code fragment that looks like this

    class ActiveSupport::TestCase
      self.use_transactional_fixtures = true
      self.use_instantiated_fixtures  = false
    end
    

    If the code should be used in all your tests, you can add your methods in the ActiveSupport::TestCase class. Otherwise, wrap them into a Module and mix the module where required.

    It’s a good practice to test validations not to test the validation itself (the validates_presence_of macro is already tested in the Rails codebase), but to make sure chances in your code don’t affect your class business.

    For instance, there might be validation which needs to be triggered only in specific events. Or you might want to make sure no one removed your super-secret validation on that specific model.

    If you want to test your model validations, I encourage you to check out shoulda. Shoulda provides several handy methods to test your validations.

    class PostTest < ActiveSupport::TestCase
    
      should belong_to(:user)
      should validates_presence_of(:id)
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small rails app that is using acts_as_list to implement drag and
I just started using CakePHP for a small project. I have rails experience, and
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I have a small rails app I got all gung-ho on tonight wanting to
Using Rails 3.2. I have an individual Shop post, and each shop has reviews.
I have a deployment using: rails 2.3.2 ruby 1.8.7 mysql db and 3 mongrel
I have such problem: I'm using rails, devise gem and jQuery validation plugin. I
I am using Rails 3.1 and want to have a Sass variable (or other
I am using rails 2.3 and have a problem. I have remote form, and
I am using rails beta 3 and I have a erb page named index.html.erb

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.