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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:00:12+00:00 2026-05-13T00:00:12+00:00

I am trying to test my views with RSpec. The particular view that is

  • 0

I am trying to test my views with RSpec. The particular view that is causing me troubles changes its appearance depending on a url parameter:

link_to "sort>name", model_path(:sort_by => 'name') which results in http://mydomain/model?sort_by=name

My view then uses this parameter like that:

<% if params[:sort_by] == 'name' %>
<div>Sorted by Name</div>
<% end %>

The RSpec looks like this:

it "should tell the user the attribute for sorting order" do
    #Problem: assign params[:sort_for] = 'name' 
    render "/groups/index.html.erb"
    response.should have_tag("div", "Sorted by Name")
end

I would like to test my view (without controller) in RSpec but I can’t get this parameter into my params variable. I tried assign in all different flavours:

  • assign[:params] = {:sort_by => 'name'}
  • assign[:params][:sort_by] = 'name'
  • …

no success so far. Every idea is appreciated.

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

    Canonical answer:

    To use params in views from within view specs, if your view has params[:id], then somewhere in your spec do:

    controller.extra_params = { id: widget.id }
    

    Documentation: https://rspec.info/features/6-0/rspec-rails/view-specs/view-spec

    However, it is also a good idea to not reference params in your views.

    To get them out of your views you could use a helper, like this:

    <div>Sorted by <%= sorted_by %></div>
    

    And in one of your helper files

    def sorted_by
        params[:sorted_by].capitalize
    end
    

    Unfortunately, you really shouldn’t be referencing params in helpers either. Don’t reference what you don’t own, ideally.

    A better idea would be to use the Presenter Pattern, and a good example of a tool for that is ViewComponent.

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

Sidebar

Related Questions

I am trying to test an ActionResult that returns a partial view. This action
I'm trying to create a detail view to display mostly textviews. I think that
I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I'm trying to use RSPec to test my Ruby on Rails 3.2 app. When
I'm trying to use capybara with rspec to test my rails app. I've simplified
I had some interesting tribulations in trying to test whether views were correctly bound
I am trying out the MVC3 Razor view engine, and one the features that
I'm trying to load views asynchronously. The problem is that the frame of the
I'm trying to test a controller to ensure that only an authorized party can
I'm trying to test some backbone.js views using Jasmine (via jasmine-headless-webkit). Everything is working

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.