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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:08:14+00:00 2026-05-25T14:08:14+00:00

Today I ran into an issue using RoR to stub calls to AR objects.

  • 0

Today I ran into an issue using RoR to stub calls to AR objects. I thought that I’d be able to do something along the lines of :

stub.instance_of(BankAccount).save_to_other_spot { true }

However when I tried this method it didn’t seem to stub the method at all and it would end up running the original method I was trying to stub. I confirmed this using debugger etc.

So I ended up using the following method :

stub.proxy(BankAccount).find(anything) do |account|
  stub(account).save_to_other_spot { true }
  account
end

This works.

I was wondering if I’m doing something wrong though? Why doesn’t instance_of work in the way I expect?

Another issue I ran into was that in my RSpec tests I seem to have to setup my mocks and stubs for each request. Again, is this normal or am I doing something wrong?

By this I mean I’d have to do something like :

... mock and stub ...
get :show, :id => @id

... mock and stub ...
post :update, :id => id, :account => { ... params ... }

I thought I’d be able to mock and stub once at the top.

  • 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-25T14:08:15+00:00Added an answer on May 25, 2026 at 2:08 pm

    Assuming you are running RSpec >2.5… then the mock/stubbing syntax has been improved so that you can now use the following definition.

    BankAccount.any_instance.stub(:save_to_other_spot) { true }
    

    Note you will need to use a latter version of RSpec. The earlier versions of RSpec do not include the any_instance method. It looks like they borrowed this from Mocha and implemented it into RSpec mocks.

    If you are using the older versions of RSpec, then what you’re doing I think is the only way. Only that I tend to write it like this:

    @bank_account = BankAccount.new
    BankAccount.stub(:find) { @bank_account }
    @bank_account.stub(:save_to_other_spot) { true }
    

    Albeit I think your block method looks cleaner.

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

Sidebar

Related Questions

Today I ran into an issue with LINQ to objects (not SQL) that popped
Ran into an issue today that I have not been able to resolve. I
I ran into an issue using a struct today that caught me off guard,
I just started playing around with threading today and I ran into something that
I ran into an interesting issue today. I have canvas elements that I am
I ran into an interesting (and very frustrating) issue with the equals() method today
Today I ran into the following problem with NUnit. I have a class, that
So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber =
I tried using PHPDoc for the first time today and quickly ran into a
I ran into an interesting dilemma today. I have a function that handles information

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.