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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:17:20+00:00 2026-06-03T11:17:20+00:00

I have this situation, in which I define an instance method which returns a

  • 0

I have this situation, in which I define an instance method which returns a “safe to tamper” testing dummy of the class itself.

require 'test/unit'
require 'shoulda'

module TestExtension
  def provides_tested_class_dummy
    self.class_exec do
      def tested_class_dummy
        txt = "class #{@tested_class.name}Dummy < #{@tested_class.name}; end"
        sub = self.singleton_class.class_eval txt
        duplicated = sub.dup
        duplicated.class_exec do
          # do evil things to the dummy,
          # such as redefining constants and stubbing methods
        end
      end
    end
  end
end

Test::Unit::TestCase.extend TestExtensions

Pleas don’t laugh yet. I’m doing this because it works for what I need to do, namely to test classes and do bad things to their dummies without touching the the tested classes in any way.

class SomeClass
  # define some constants
  # define some methods
end

Then, testing it:

class SomeClassTest < Test::Unit::TestCase
  @tested_class = ::SomeClass
  provides_tested_class_dummy()

  should "somehow comply" do
    dummy = tested_class_dummy()
    assert_kind_of Module, dummy
    # other assertions about the dummy's behavior
  end
end

Now you can start laughing.

My main question is, in the line “duplicated = sub.dup”, where I duplicate the subclassed tested class, does that line make any sense? From me, it’s voodoo, I’m doing it just to minimize any negative effects of the tampering done to the dummy on the tested class itself. Am I gaining anything by #dup-ping a class?

Side question: If I’m doing something blatantly laughable, please tell me. I simplified this code and I am not really sure how it would work (the more complicated original does work as intended for me), but I hope you can at least get the idea. Also, please, wizards there, recommend me a good mocking framework.

  • 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-03T11:17:21+00:00Added an answer on June 3, 2026 at 11:17 am

    If you want to do things like change constants or other class-wide data without trampling on the original class, dupping it looks to be the way to go –

    class Lion
      ROAR = 'roar!'
    end
    
    L2 = Lion.dup
    
    puts Lion::ROAR             # "roar!"
    puts Lion::ROAR.object_id   # 2152285120
    
    puts L2::ROAR               # "roar!"
    puts L2::ROAR.object_id     # 2152285120
    
    L2::ROAR = 'rawrrr'
    
    puts Lion::ROAR             # "roar!"
    puts Lion::ROAR.object_id   # 2152285120
    
    puts L2::ROAR               # "rawrrr"
    puts L2::ROAR.object_id     # 2152285000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this situation today. I have an object which I'm testing for
We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {
I have a Java class in which every method was defined as static .
My situation I have an entity model with a base class ItemBase which defines
I have a situation in which A has a reference to a class C
I have a div which has some text, but according to different situations this
I have this istuation. I have a setTimeout to a function in which I
I have this situation: { float foo[10]; for (int i = 0; i <
I have this situation where I want to display a list of Administration objects

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.