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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:41:35+00:00 2026-05-28T20:41:35+00:00

I am trying to mock an array in RSpec (in the app it’s a

  • 0

I am trying to mock an array in RSpec (in the app it’s a return object from an external API), yet I don’t know how.

I tried mocking it like this:

item = double("item")
item.stub(:[]) { :return_value }

which works, but then I’ll get :return_value for each value in the array.

Is there another way?

  • 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-28T20:41:35+00:00Added an answer on May 28, 2026 at 8:41 pm

    I think you don’t need to generate test doubles for array, they will add unnecessary complication to the code of your tests. You can just create fake array and use it later:

    items = [:return_value1, :return_value2]
    

    In case if you need to stub method and return different results for first and subsequent calls, you can do this:

    obj.stub(:method).and_return('1', '2')
    

    In this case obj.method will return ‘1’ when it is called for the first time and return ‘2’ for all subsequent calls.

    Also, as far as you use block for stub, you can dynamically calculate return value in this block. But this is considered to be not very good practice because idiomatically stubs should return static data.

    obj.stub(:method) { Time.now }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to mock the RabbitMQ ConnectionFactory object to return a mocked connection, using
When trying to mock a Faraday::Response object, I've tried: mock(Faraday::Response, :env => {:body =>
I am trying to Mock an object that is being passed into another object,
I'm trying to create a mock object of \SplObserver using PHPunit and attach the
Trying to mock the following method: bool IsLoginValid(LoginViewModel viewModel, out User user); Tried this
I am trying to create a mock object in PHP and PHPUnit. So far,
Im trying to mock a method that is ineherited from a parent class that
I'm trying to mock a SqlDataReader SqlDataReader reader = mocks.CreateMock<SqlDataReader>(); Expect.Call(reader.Read()).Return(true).Repeat.Times(1); Expect.Call(reader.Read()).Return(false); Expect.Call(reader.HasRows).Return(true); Expect.Call(reader.Dispose);
I'm trying to mock a data repository object but after setting an expectation on
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using

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.