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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:28:52+00:00 2026-06-18T12:28:52+00:00

I have a project in which I use ActiveRecord to store information in a

  • 0

I have a project in which I use ActiveRecord to store information in a sqlite db file. I’m not using Rails and AR seems to do the job perfectly. My question is how exactly to test my classes witout hitting the db? I found some gems that would to the trick (FactoryGirl, UnitRecord), but they are meant to work with Rails.

class News < ActiveRecord::Base
belongs_to :feed

def delete_old_news_if_necessary
  # time_limit = Settings::time_limit
  return if time_limit.zero?

  News.destroy_all("date < #{time_limit}")
end

def delete_news_for_feed(feed_id)
  News.destroy_all(:id => feed_id)
end

def news_for_feed(feed_id)
  News.find(feed_id)
end
end

I read that i can do a column stub:

Column = ActiveRecord::ConnectionAdapters::Column
News.stubs(:columns).returns([Column.new(),...])

Is this the right way to do these tests? Also, when is it better to have a separate db just for testing and to create it, run the tests, and the delete it?

  • 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-18T12:28:53+00:00Added an answer on June 18, 2026 at 12:28 pm

    If you want to avoid hitting the db in tests I can recommend the mocha gem. It does stubs as well as it lets you define expectations.

    Edit: Regarding your question on when it is better to use a test db: I would say, whenever there is no reason against it. 🙂

    Edit: For example, you can mock News.find like this in a test:

    def news_for_feed_test
      # define your expectations:
      news = News.new
      News.expects(:find).with(1).returns(news)
      # call the method to be tested:
      News.new.news_for_feed(1)
    end
    

    At the same time this makes sure, find gets called exactly once. There are a lot more things Mocha can do for you. Take a look at the documentation. Btw., it looks like these methods of yours should be class methods, no?

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

Sidebar

Related Questions

I have a project in which i use JDBC with MySQL to store some
I have a project which exposes object model to use by different types of
I have an existing project repo which I use for project A, and has
I have a project which I am trying to use in another new project,
I have a project in which I'd like to use some of the .NET
I use LINQ-SQL as my DAL, I then have a project called DB which
In my testing project, I have a static class called FixtureSetup which I use
I have a DLL project in VS 2008 Pro which uses OpenMP. I use
I've begun using ReSharper and really enjoy it. However I have a project which
I have built a simple project which use Winsock Tool. When I receive any

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.