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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:05:06+00:00 2026-05-15T08:05:06+00:00

Given the Thread class with it current method. Now inside a test, I want

  • 0

Given the Thread class with it current method.
Now inside a test, I want to do this:

def test_alter_current_thread
  Thread.current = a_stubbed_method
  # do something that involve the work of Thread.current
  Thread.current = default_thread_current
end

Basically, I want to alter the method of a class inside a test method and recover it after that.
I know it sound complex for another language, like Java & C# (in Java, only powerful mock framework can do it). But it’s ruby and I hope such nasty stuff would be available

  • 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-15T08:05:06+00:00Added an answer on May 15, 2026 at 8:05 am

    You might want to take a look at a Ruby mocking framework like Mocha, but in terms of using plain Ruby it can be done using alias_method (documentation here) e.g.

    beforehand:

    class Thread
      class << self
        alias_method :old_current, :current
      end
    end
    

    then define your new method

    class Thread
      def self.current
        # implementation here
      end
    end
    

    then afterwards restore the old method:

    class Thread
      class << self
        alias_method :current, :old_current
      end
    end
    

    Update to illustrate doing this from within a test

    If you want to do this from within a test you could define some helper methods as follows:

    def replace_class_method(cls, meth, new_impl)
      cls.class_eval("class << self; alias_method :old_#{meth}, :#{meth}; end")
      cls.class_eval(new_impl)
    end
    
    def restore_class_method(cls, meth)
      cls.class_eval("class << self; alias_method :#{meth}, :old_#{meth}; end")
    end
    

    replace_class_method is expecting a class constant, the name of a class method and the new method definition as a string. restore_class_method takes the class and the method name and then aliases the original method back in place.

    Your test would then be along the lines of:

    def test
      new_impl = <<EOM
      def self.current
        "replaced!"
      end
    EOM
      replace_class_method(Thread, 'current', s)
      puts "Replaced method call: #{Thread.current}"
      restore_class_method(Thread, 'current')
      puts "Restored method call: #{Thread.current}"
    end
    

    You could also write a little wrapper method which would replace a method, yield to a block and then ensure that the original method was reinstated afterwards e.g.

    def with_replaced_method(cls, meth, new_impl)
        replace_class_method(cls, meth, new_impl)
        begin
            result = yield
        ensure
            restore_class_method(cls, meth)
        end
        return result
    end
    

    Inside your test method this could then be used as:

    with_replaced_method(Thread, 'current', new_impl) do
      # test code using the replaced method goes here
    end
    # after this point the original method definition is restored
    

    As mentioned in the original answer, you can probably find a framework to do this for you but hopefully the above code is interesting and useful anyway.

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

Sidebar

Related Questions

Given: Object innerProxy = ... Object proxy = java.lang.reflect.Proxy. newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{type}, innerProxy); How
I presumed that a pthread_t remains constant - for a given thread - for
I have a class that has two method in it, one calls a class
Given a Grails 1.3.7 taglib as follows, package grailsworld class FooTagLib { def bar
After following the great advice given in a thread about service beans I have
Given client = new XMemcachedClient(server, port); What happens if I have several thread doing
Given the following code. EventLoopScheduler scheduler = new EventLoopScheduler(ts => new Thread(ts)); BehaviorSubject<int> subject
Is it possible to get a list of thread handles at any given time
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
earlier today I asked this question . So since moq creates it's own class

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.