Question

I´m trying to test a view which uses will_paginate but I´m getting a unexplainable No route matches error. Note that visiting stations/1/measures/page/2 via browser or controller / feature spec works.

routes.rb

  ...

  resources :stations, shallow: true do
    resources :measures, only: [:show, :create, :destroy]
  end
  get "/stations/:station_id/measures/page/:page", to: "stations#measures", as: :station_measures_paginate

  ...

views/stations/measures.erb.html

    <div class="row">
      <h1>Latest measures for <%= @station.name.capitalize %></h1>
      <p>Latest measurement recieved at <%= time_date_hours_seconds(@measures.last.created_at) %></p>
      <%= render :partial => 'measures/table', object: @measures %>

      <%= will_paginate @measures %>
    </div>

These specs:

spec/views/stations/measures.html.erb_spec.rb

    require 'spec_helper'

    describe "stations/measures" do

      let!(:station) { build_stubbed(:station) }

      let!(:measures) do
        [*1..30].map! { build_stubbed(:measure, station: station) }.paginate
      end

      before(:each) do
        Measure.stub(:last).and_return(measures.last) 
        assign(:station, station)
        assign(:measures, measures)
        stub_user_for_view_test
      end

      subject {
        render
        rendered
      }

      it { should match /Latest measures for #{station.name.capitalize}/ }
      it { should match /Latest measurement recieved at #{measures.last.created_at.strftime("%H:%M")}/ }
      it { should have_selector '.pagination' }

    end

Give the following error three times:

  1) stations/measures 
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:page=>2, :controller=>"stations", :action=>"measures"}
     # ./app/views/stations/measures.html.erb:6:in `_app_views_stations_measures_html_erb___1173544536802428572_70225207801140'
     # ./spec/views/stations/measures.html.erb_spec.rb:23:in `block (2 levels) in <top (required)>'
     # ./spec/views/stations/measures.html.erb_spec.rb:27:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'


    ...
Was it helpful?

Solution

The reason is you need to feed the route with station_id as you defined in routes.rb. There is no such thing in the params shown in question.

I have never written view tests in Rspec-rails and don't know how to adapt to this route. I would suggest you to use controller test instead, add render_views macro to allow view rendered and then have access to response.body.

Since the pagination logic is mostly implemented in controller, I think it's totally reasonable to categorize this test in controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top