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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:57+00:00 2026-05-27T19:10:57+00:00

Using ruby(1.8.7) , rails(2.3.8) , rspec(1.3.1) and rspec-rails(1.3.3) how do I test a method

  • 0

Using ruby(1.8.7), rails(2.3.8), rspec(1.3.1) and rspec-rails(1.3.3) how do I test a method that is full of complex SQL queries? Is there a way to make use of stubs and mocks. For example, a method like:

class Bucket << ActiveRecord::Base
  attr_accessor :students
  def populate_students_subassesmentwise(subassesment, mentor)
    student_ids = Mark.find(:all,
      :joins => "#{self.student_current_klass} #{self.current_mentors}",
      :conditions => ["marks.subject_id = ? AND st.klass_id = ? AND 
                   IF(sub.is_elective IS TRUE, 
                     (se.student_id = st.id AND se.subject_klass_id = marks.klass_id AND se.student_klass_id = st.klass_id AND marks.subject_id = se.subject_id),
                     (marks.klass_id = st.klass_id))
                   AND u.account_enabled IS TRUE AND sub.active IS TRUE AND k.active IS TRUE AND marks.markable_id = ? AND marks.markable_type = ?
                   AND ROUND(marks_obtained/marks_total*100) BETWEEN ? AND ? ",
                   mentor.subject_id, mentor.klass_id, subassesment.id, "Subassesment", min_range, max_range],
      :group => "marks.id").collect(&:student_id)

    self.assign_students(student_ids) # This is a call to another instance method that runs a query to find the students having ids equal to student ids and assign it to the students attr_accessor
  end
end

As you can see there are a lot of JOIN, IF and WHERE clauses, that are more of talking in ‘DBMS’ terms, how do I write tests in Rspec that would mock this query? Or should we use fixtures?

  • 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-27T19:10:57+00:00Added an answer on May 27, 2026 at 7:10 pm

    Personally I would test this using fixtures (or rather, FactoryGirl). Since you’re really testing your sql/database code it seems silly to me not to test this in the database. Unless you have a seperate sql unittesting setup or something (which seems like overkill to me 😉 (in most scenarios))

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

Sidebar

Related Questions

I'm using Ruby on Rails to make an application where businesses can create accounts
I'm using ruby on rails 2.3.2 and also using the acts_as_taggable_on puglin. That generated
How can I start using Rspec with an existing Ruby on Rails 2.3.5 project
I'm following the ruby on rails tutorial: http://railstutorial.org/chapters/static-pages#top I'm up to using rspec. Having
I'm using RSpec for my Rails 3 tests and trying to use Spork. I
I'm using rr (the mocking framework) and rspec with ruby-on-rails. Also, I'm using the
I am using Ruby on Rails 3.0.9 and RSpec 2. I would like to
I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I am
I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would
I am using Ruby on Rails 3.0.9 and RSpec 2. I would like to

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.