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

  • Home
  • SEARCH
  • 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 8066513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:01:36+00:00 2026-06-05T12:01:36+00:00

I have an ActiveRecord model which has some callbacks and custom validations I want

  • 0

I have an ActiveRecord model which has some callbacks and custom validations I want to test. It means, that I have to make all of them public. Is that a good practice or is there a better solution in this case?

Examples of the methods I want to test:

  def before_validation
    original.edit(self) if original
  end

  validate :unique?, on: :create

  def unique?
    return true if original.blank?
    errors.add(:base, 'The entry already exists')
    false
   end

In other words:

  1. If I want to test “before_validation” method, do I have to make it public and call test_object.before_validation directly in my model Rspec file?
  2. If I want to test not only, that custom validation method unique?() is called on validation, but also, the method itself, do I have to make the unique?() public as well?

The problem is, that neither of these methods I am using outside my of model (so they they should be private), but how could I test without making them public?

  • 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-06-05T12:01:41+00:00Added an answer on June 5, 2026 at 12:01 pm

    It’s easy to get caught up in testing implementation, but this is a good candidate for testing behaviour.

    Ask yourself, what is your before validation really doing? Then make yourself a test that, given certain inputs, you’ll get certain outputs if and only if your before validation works.

    As a simple example, imagine you have a user model with a simple name attribute, and you have a before_validation that simply does:

    name = "Auto-generated name." if name.blank?
    

    Then you could write a test for it like this:

    it "gets an auto-generated name if none is specified." do
        user = User.create!
        user.name.should eq "Auto-generated name."
    end
    
    it "does not get an auto-generated name if one is specified." do
        user = User.create!(:name => "Test Name")
        user.name.should eq "Test Name"
    end
    

    So as you can see, this test does test the validation, but it doesn’t need it to be public, because you’re just testing the behaviour of it. The same goes for your unique? method. Give it certain inputs and test the behaviour, without having to care about which specific method was used or any other implementation details.

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

Sidebar

Related Questions

I have a Query model: class Query < ActiveRecord::Base belongs_to :test end which is
I have a model Journey which has many Users (drivers). I want to be
I have an ActiveRecord Appointment model, which includes topic and time attributes. I would
I have a model in my rails application which is class Person < ActiveRecord::Base
I have a process which iterates through a bunch of ActiveRecord models, does some
I have a model called Category which looks like this: class Category < ActiveRecord::Base
In my rails 3.1 project I have a Book model that has a ID,
I have a tree-like model where in all situations but one , I want
I have an ActiveRecord model whose fields mostly come from the database. There are
I have an ActiveRecord model with a field called name (in the database) and

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.