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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:30:39+00:00 2026-05-17T00:30:39+00:00

In Rails (3.0) test code, I’ve cloned an object so I can clobber it

  • 0

In Rails (3.0) test code, I’ve cloned an object so I can clobber it for validation testing without changing the original. If I have called assert(original.valid?) before cloning, then the clone passes the validates_presence_of test even after I have set member_id value to nil.

The two tests below illustrate this. In test one, the clone is created before the original (“contact”) is validated. Clone correctly fails the validation when member_id is missing. Assertion C succeeds.

In test two, the clone is created after the original is validated. Even though clone.member_id is set to nil, it passes the validation. In other words, assertion 2C fails. The only difference between the tests is the order of the two lines:

  cloned = contact.clone
  assert(contact.valid?,"A")

What is going on here? Is this normal Ruby behavior re: cloning that I just don’t understand?

test "clone problem 1" do
  contact = Contact.new(:member_id => 1)
  cloned = contact.clone
  assert(contact.valid?,"A")
  cloned.member_id = nil
  assert(!cloned.valid?,"C")
end

test "clone problem 2" do
  contact = Contact.new(:member_id => 1)
  assert(contact.valid?,"2A")
  cloned = contact.clone
  cloned.member_id = nil
  assert(!cloned.valid?,"2C")
end
  • 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-17T00:30:40+00:00Added an answer on May 17, 2026 at 12:30 am

    You wil be surprised – it cannot work!

    Ok the reason can be found in the Rails code. First validation will run the code:

    # Validations module
    
    # Returns the Errors object that holds all information about 
    # attribute error messages.
    def errors
      @errors ||= Errors.new(self)
    end
    

    As it is a first run then it will create new instance of Errors class. Simple, isn’t it? But there is a gotcha – the parameter is self. In your case it is “contact” object.

    Later then when you call this again on cloned object, the @errors instance will not be created again – as it is not null. And there it is! Instead of passing “cloned” self, the older self is used.

    Later in the validation code there the Errors class runs the code that read the value from @base which is the self from the initialization. Can you see it? The values for test are read from original model not from the clone! So the validation on “cloned” object runs on values from the original.

    Ok, so far for the “why not” and now a few words about “how to”.

    The solution is simple – just set @errors to nil after cloning and before validation. As it is quite private, the simple assignment doesn’t work. But this works:

    cloned.instance_eval do
      @errors = nil
    end
    

    And some tip for interesting reading: http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/

    It is quite comprehensive explanation how the validations in Rails 3 works.

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

Sidebar

Related Questions

I have a Rails controller action to test. In that action, a method User.can?
I'm currently looking at a hefty Rails test suite. It's nothing I can get
I could have sworn there was a way to see Rails test output in
Does anyone know how to test page_caching in Rails using RSpec without having to
I just cloned the rails source code from github, and attempted to run the
I have a bunch of Rails 3.1 controllers which all have very similar testing
Below I listed some code from simple Rails application. The test listed below fails
I have a functional test suite failing in a Rails 2.2.2 application. The exception
I would like test code in the lib directory of Rails. I use RubyTest
So I'm using rspec to test my code as I'm going through the Rails

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.