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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:09:30+00:00 2026-06-17T15:09:30+00:00

I am new to testing with RSpec and I would really appreciate your help.

  • 0

I am new to testing with RSpec and I would really appreciate your help.

This is my index action inside the controller and I need to make tests for it to see if the chained selects inside the view are acting as they are supposed to. I would be grateful for any code you could provide me with, since I am not even sure where to start with this.

def index
 respond_to do |format|
  format.html
  format.json do
    if params[:s_id].blank? && !params[:f_id].blank?
      @fp_for_dropdown = Model.where(:f_id => params[:f_id])
    elsif !params[:s_id].blank? && params[:f_id].blank?
      @fp_for_dropdown = Model.where(:s_id => params[:s_id])
    elsif !params[:s_id].blank? && !params[:f_id].blank?
        @fp_for_dropdown = Model.where(:s_id => params[:s_id], :f_id => params[:f_id])
    else
      @fp_for_dropdown = Hash[]
    end
    render :json => Hash["" => ""].merge(Hash[@fp_for_dropdown.map { |i| [i.id, i.name] }])
  end
end

end

  • 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-17T15:09:32+00:00Added an answer on June 17, 2026 at 3:09 pm

    One approach to this problem is to think of your controller tests as unit tests. That means mocking out the calls to Model to make sure the correct ones are called.

    describe SomeController do
      it 'should retrieve by s_id only' do
        Model.should_receive(:where).with(:s_id => 'abc')
        get :index, :s_id => 'abc'
      end
      it 'should retrieve by f_id only' do
        Model.should_receive(:where).with(:f_id => 'abc')
        get :index, :f_id => 'abc'
      end
      it 'should retrieve by f_id and s_id' do
        Model.should_receive(:where).with(:f_id => 'abc', :s_id => '123')
        get :index, :f_id => 'abc', :s_id => '123'
      end
      it 'should not hit the database without f_id or s_id' do
        Model.should_not_receive(:where)
        get :index
      end
    end
    

    If you want to get fancy, you can also test the response json:

    it 'should retrieve by f_id only' do
      Model.should_receive(:where).with(:f_id => 'abc').and_return(OpenStruct.new(id: 'abc', name: '123'))
      get :index, :f_id => 'abc'
      JSON.parse(response.body).should == {} # INCOMPLETE, fill in response to expect
    end
    

    Unfortunately, I couldn’t complete the response checking because I do not know what you are doing with the Hashes in this line:

    render :json => Hash["" => ""].merge(Hash[@fp_for_dropdown.map { |i| [i.id, i.name] }])
    

    Take a look at jbuilder or rabl for constructing JSON responses with views.

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

Sidebar

Related Questions

Just need alittle bit of help with rspec testing... very new to it and
I am testing a simple password reset action and would like RSpec's change matcher
This is from Michael Hartl's book, section 8.4. RSpec is testing a successful signup
I am new to rspec and wanted to ask how one would go about
I am new in testing, and to be honest I would like to start
I am running rails 3.1.3 with postgresql 9.1 and testing with Rspec. This is
I recently learned, Rspec + Capybara (I'm new to testing things). And I've been
I'm working on testing my login page via rspec using this script describe Sessions
I am new to testing Rails web applications and RSpec. I work with legacy
I'm new to the concept of testing and there's one thing that's really putting

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.