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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:31:44+00:00 2026-05-19T12:31:44+00:00

My Rails 3 application has a Product resource. In my ProductsController specs, the generated

  • 0

My Rails 3 application has a Product resource.

In my ProductsController specs, the generated tests are making sure that I redirect to a product’s url afer updating it.

The following code errors:

#inside ProductsController spec
def mock_product(stubs={})
  (@mock_product ||= mock_model(Product).as_null_object).tap do |product|
    product.stub(stubs) unless stubs.empty?
  end
end

it "should do something" do 
  product_url(mock_product)
end

The error is:

TypeError: RSpec::Mocks::Mock#to_hash should return Hash
  • 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-19T12:31:44+00:00Added an answer on May 19, 2026 at 12:31 pm

    My mock_model is defined as_null_object (which means that it will respond to all messages, except those stubbed, by returning itself.

    So, rails routing helpers (like product_url(product)) first converst the product to a hash, and uses the product[‘id’] to generate the route.

    Since my mock_product didn’t stub to_hash, the “as_null_object” behavior took precedence

    irb > mock_product
    => #<Product:0x..fdb3b6176 @name="Product_1028">
    irb > mock_product.to_hash
    => #<Product:0x..fdb3b6176 @name="Product_1028">
    

    My solution was to stub to_hash:

    irb > mock_product(:to_hash => {:id => 1 }).to_hash
    => {:id=>1}
    

    Should this be included in the mock_product model by default?

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

Sidebar

Related Questions

I have a rails application that has three different types of users and I
in my Rails 3.1, ruby 1.9.2 application, I've an Object 'Post' that has several
I am working on a Rails application that has user authentication which provides an
We are developing an application that has an iPhone client, and a Rails server.
I have a Rails application that has to co-exist with a very old legacy
I have a Rails application that has file uploading. Once the file is uploaded
My Rails application has a simple yaml file that's downloaded relatively frequently (5x per
I have an application, built with Rails 3.1.3, that has products and categories. The
I'm developing a Ruby on Rails application that has table attributes with special requirements.
Got a rails application that has a form (new action) with a select with

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.