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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:55:07+00:00 2026-05-16T19:55:07+00:00

How can I mock the initialize method on a ruby class? I’m doing some

  • 0

How can I mock the initialize method on a ruby class?

I’m doing some testing and want to mock out the object that is created from a new call.

I tried to write a few things and none of them seemed to get the mock class to return from the new call. It just keeps returning the normal, expected object.

EDIT:

one attempt –

class MockReminderTimingInfoParser < ReminderTimingInfoParser
  def new(blank)
    ReminderTimingInfoParserForTest.new
  end
end

describe ReminderParser do
  parser = ReminderParser.new(MockReminderTimingInfoParser)

  it "should parse line into a Reminder" do
    parser.parse(" doesnt matter  \"message\"").should == Reminder.new('message', ReminderTimingInfo.new([DaysOfWeek.new([:sundays])], [1]))
  end
end

class ReminderTimingInfoParserForTest
  include TimingInfoParser

  def parse_section(section); [DaysOfWeek.new([:sundays]), 1] end

  def reminder_times_converter(times); times end
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-16T19:55:08+00:00Added an answer on May 16, 2026 at 7:55 pm
    class MockReminderTimingInfoParser < ReminderTimingInfoParser
      def new(blank)
        ReminderTimingInfoParserForTest.new
      end
    end
    

    Here, you are defining a method called new for all instances of the class MockReminderTimingInfoParser. In your question, you mention that you want to hook into instance creation. However, in Ruby, instance creation is not done by instance methods. Obviously, this cannot work, since in order to call an instance method, you’d need to have an instance first!

    Instead, instances are created by calling a factory method (commonly called new) on the class.

    In other words, in order to create an instance of MockReminderTimingInfoParser, you would call MockReminderTimingInfoParser.new, but you have defined a method MockReminderTimingInfoParser#new. In order to call the method you have defined, you would have to call MockReminderTimingInfoParser.new.new.

    You need to define a method on MockReminderTimingInfoParser‘s singleton class. There’s several ways to do that. One way would be just mimicking the way you would call the method:

    def MockReminderTimingInfoParser.new(blank)
      ReminderTimingInfoParserForTest.new
    end
    

    Another would be opening up MockReminderTimingInfoParser‘s singleton class:

    class << MockReminderTimingInfoParser
      def new(blank)
        ReminderTimingInfoParserForTest.new
      end
    end
    

    However, in both of these cases, MockReminderTimingInfoParser obviously has to exist first. Given that you need to define the class anyway, here’s the most idiomatic way of defining methods on a class’s (or module’s) singleton class:

    class MockReminderTimingInfoParser < ReminderTimingInfoParser
      def self.new(blank)
        ReminderTimingInfoParserForTest.new
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to find out how I can mock a method that returns a
I want to create an object that acts as a specific class, such as
Is there any way that we can mock certain methods using existing objects ?
I was wondering how I can mock the ClaimseReponse class in DotNetOpenAuth? This is
I want to make a mock installer using NSIS so we can demo what
I have a class Foo that uses another class Bar to do some stuff.
I tried to mock some maven classes. I know I can mock an Interface
Using PHPUnit, I wonder if we can mock an object to test if a
Possible Duplicate: How to mock a method that returns an int with MOQ Here's
I took a method from this post (accepted answer code) that serializes object so

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.