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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:10:09+00:00 2026-06-12T00:10:09+00:00

Note: I’m only posting this question so that others may find it if they

  • 0

Note: I’m only posting this question so that others may find it if they ever need to, as I have found a good solution.

In my controller tests, I don’t want to commit to the database, but I still want the controllers to use finder methods to get mock objects (by mocking the find method), but then I want to use dom_id on those mocks in assert_select, to verify that they’re being displayed.

However, since they’re non-saved objects, dom_id keeps returning new_object instead of object_1, object_2, et cetera.

Is there any quick way to get it to work? I really don’t want to persist real records in tests.

  • 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-12T00:10:11+00:00Added an answer on June 12, 2026 at 12:10 am

    Suppose you’re using ActiveSupport::TestCase and FactoryGirl for building model objects, you can add this to your test/test_helper.rb file:

    # Generate model with id - useful for assert_select with dom_id
    def model(name, options = {})
      @next_id = {} if @next_id.nil?
      @next_id[name] = 1 unless @next_id.has_key(name)
    
      m = build(name, options)
      m.id = @next_id[name]
      @next_id[name] += 1
      m
    end
    

    Then, suppose you’re using RR as your mocking library, you can do something like this:

    test 'something' do
      posts = [model(:post), model(:post)]
      mock(Post).all { posts }
      get :index
      assert_select "##{dom_id(posts.first)}", posts.first.title
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: This is a followup to my question here. I have a program that
NOTE: This question is only loosely related to tinyxml, however including details like that
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
Note: To answer this question, you shouldn't have to know anything about Selenium or
NOTE: This is an entire rewrite of the previous question. I have a model.
NOTE: I have updated this since originally asking the question to reflect some of
note: I'm working on wordpress but this isn't a wordpress question. I have a
Note: I already saw this and it doesn't answer the question. I have a
NOTE: This question has been renamed. Originally it targeted the fact that I was
(Note: This is not a question about what is the best way with code

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.