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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:31:36+00:00 2026-06-18T10:31:36+00:00

I’m trying to test the behavior of a custom search method in my controller:

  • 0

I’m trying to test the behavior of a custom search method in my controller:

    #RecordingsController
    def search
      # raise params.inspect
      @search = params[:search]
      searches = []
      searches2 = []
      for n in 1..5
        searches << @search["date(#{n}i)"].to_i
        searches2 << @search["date2(#{n}i)"].to_i
      end
      start_date = date_format(*searches)
      end_date = date_format(*searches2)
      conditions = []
      conditions << "agent like '%#{@search["agent"]}%'" unless @search["agent"].blank?
      conditions << "phone like '%#{@search["phone"]}%'" unless @search["phone"].blank?
      conditions << "date between '#{start_date}' and '#{end_date}'"

      @recordings = Recording.where(conditions.join(" AND ")).order('date ASC')
      if @recordings.blank?
        redirect_to("/", alert: "No results were found for this search. Please try again.")
      else
        render "recordings/search"
      end
    end

using the following layout:

    #recordings_controller_spec.rb
    describe RecordingsController do
      describe "POST #search" do
        context "with valid attributes" do
          it "assigns a new search to @search" do
            search = @recording_search
            get :search, @recording_search
            assigns(:search).should eq(search)
          end
          it "populates an array of recordings"
          it "renders the :search view"
        end
      end
    end

The furthest I’ve gotten is trying to build a hash that mimics what my params hash would be for the form

    #params hash
    params = {"search" => { "date_1i" => "2012", "date_2i" => "1", ... "date2_5i" => "00" } }

where date_#{n}i is the start date [year, month, day, hour, minute], and date2_#{n}i is the end date. I’m trying to follow the answer posted here, mimicking the params hash with just a regular hash. As you can see from my controller, I don’t actually pass parameters to my #search method. Should I be? Or is there a way to mock a params hash in an rspec test and determine if my @search, @recordings, and redirect_to/render variables/actions are being performed? I’m already kind of testing the render/redirect in my request spec, but I’d like to fully test this method if I can.

  • 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-18T10:31:38+00:00Added an answer on June 18, 2026 at 10:31 am

    You should be able to generate a controller spec that GETs the search action with a given set of parameters. This will cause those parameters to be available to the params hash. You can then verify how the search is constructed and which results are returned.

    describe RecordingsController do
      describe '#search' do
        it 'should return results' do
          get :search, "search" => { "date_1i" => "2012", "date_2i" => "1", ... "date2_5i" => "00" }
          response.should be_ok
          @recordings.map(&:name).should == ['expected1', 'expected2']
        end
      end
    end
    

    This example executes a search with some search criteria as query parameters, verifies the response is successful (http 200 status), and then extracts the list of recordings returned and tries to map them to a list of friendly recording names (you can use any key on this model) to compare them to an expected list of results.

    It’ll be easier to write/test this code if you separate the concerns in this controller – you could write a helper that processes the query parameters and builds a search filter, which it then passes to the Recording model in the controller:

    class RecordingController
      def search
        @search_filter = SearchFilter.for_params(params[:search])
        @recordings = Recording.where(@search_filter).order('date ASC')
        render "recordings/search"
      end
    end
    
    class SearchFilter
      # Return search params as a hash for given request params hash
      def self.for_params(params)
        ...
      end
    end
    

    This would let you write a unit test for the logic that generates search filters and only verify that the controller is doing the more simple operation of passing information between the search logic and the Recording model collection. I’d also recommend moving your logic about displaying empty results into the view on the results page and out of the controller.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.