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

  • Home
  • SEARCH
  • 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 7077095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:22:49+00:00 2026-05-28T06:22:49+00:00

I have a method called save_title : def save_title (data) … [ if the

  • 0

I have a method called save_title:

def save_title (data)
  ...  
  [ if the record exists, update, return 0] 
  [ if the record is new, create, return 1] 
end  

All fine, until I stubbed it:

saved_rows = []
 proc.stub(:save_title) do |arg|
  saved_rows << arg
 end

The bug here is that I was using the integer returned from the real method to determine how many records were created vs. updated. The stub doesn’t return an integer. Oooops. So the code worked fine in reality, but appeared broken in the test. A while later (more than I care to admit, cursing included) I realize the stub and the real method don’t behave the same. Such are the pitfalls of dynamic languages I suppose.

Questions:

  1. Can I tell rspec to warn me if the stub doesn’t return the same sort of thing as the real method?
  2. Is there an analyzer gem that I can use to warn about this sort of thing?
  3. Is there some sort of best practice that I don’t know about with returning values from methods?
  • 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-28T06:22:49+00:00Added an answer on May 28, 2026 at 6:22 am

    1) There is no way that rspec can know what type of object the method is supposed to return, that’s for you to tell it, however…

    2) There is something you can look into. Instead of using a stub, try using a mock instead as your test double. It is basically the same thing as a stub, however, you can do many more validations on it (check out the documentation here). Things like how many times the specific method was called, the arguments it should be called with and what the return value should be as well. Your test will fail if any of those validations don’t pass.

    3) The best practice would be the method name itself. For example, methods ending in ? like object.exists? should always return a boolean value. In your case, I would suggest a refactoring of your method, maybe divide it in two, one for updating and one for creating and have another method to tell you if an object exists or not. It is not good practice to have a method behave in two different ways depending on the input (see separation of concerns)

    Good luck! hope this helps.

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

Sidebar

Related Questions

I have recently found out that there exists a method called nth_element in the
If you have a Point3 class and have a method called Distance , should
I have a method that is called addHighScore. When a user wants to quit
I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
I have a controller which has a method called history class UsersController < ApplicationController
I have the following bit of code in a method called by clicking the
Suppose that we have a class called class1. The class1 has a method called
So I have a very simple class that has a method called getThumbUrl() but
I have a .dll file and in there, there's a method called A. When
Let's say I have a java.util.Properties object. The Properties object has a method called

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.