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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:10:14+00:00 2026-06-02T19:10:14+00:00

I have a class method which I would like to mock and return one

  • 0

I have a class method which I would like to mock and return one of the arguments that was passed in. Something like this in my code:

converted_data = Myclass.convert(arg, some_other_arg, data)

And in my test I would like to be able to do the following (although this doesn’t work).

Myclass.should_receive(:convert).with(*args).and_return(args[2])

So the method doesn’t actually do anything! If I run as written above, I get an error that it doesn’t know what args is to return it.

  • 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-02T19:10:17+00:00Added an answer on June 2, 2026 at 7:10 pm

    I found the answer:

    @something.should_receive(:method).with(any_args()).and_return { |*args| args }
    

    To return a specific argument, pick it from the array within the and_return block! So for your example, it would be:

    Myclass.should_receive(:convert).with(any_args()).and_return { |*args| args[2] }
    

    You likely want to do this on an instance of Myclass.

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

Sidebar

Related Questions

I would like to have my abstract parent class have a method that would
I have a class which I would like to test with a public method
I have a validation class which uses method chaining. I would like to be
I have a class that has an output() method which returns a matplotlib Figure
I have class which have one public method Start , one private method and
I would like to mock a class (i.e. with RhinoMocks) for which I don't
I have a method that contains an asynchronous call like this: public void MyMethod()
I have a class with static method which has a local static variable. I
I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
I have an abstract class which contains method for setting header text. It looks

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.