View spec with nested resource gives “No route matches” #355

maxcal opened this Issue Jan 24, 2014 · 4 comments

2 participants


When using will paginate on a view that includes a nested resource it breaks my view specs.
The view however works fine when visited by browser or feature specs.
It seems that the parent resource (station) is not included when calling url_for. I have been able to isolate it by doing:

before :each  { view.stub(:url_for) }

The RSpec error message

2) stations/measures 
     Failure/Error: render
       No route matches {:page=>2, :controller=>"stations", :action=>"measures"}
require 'spec_helper'

describe "stations/measures" do

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

  let!(:measures) do
    # page, per_page, total_entries
    WillPaginate::Collection.create(1, 10, 50) do |pager|
      pager.replace([*1..50].map! { build_stubbed(:measure, station: station) })


  before(:each) do
    assign(:station, station)
    assign(:measures, measures)

  subject {

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

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

  <%= will_paginate @measures %>

Did you fix this @maxcal? I'm suffering the same problem.


@Senen yes, but it was a while ago. Don´t remember exactly how I fixed it but i think it involved using locals in the partial which was paginated:

<%= render partial: 'observations/table',
           locals: { observations: @observations, station: @station }

<table class="observations small-12 columns">
  <th>Wind Speed</th>
  <%= render partial: "observations/observation",
             collection: observations,
             locals: { station: station } if observations.any?

specs are here:


Thank you @maxcal,

I was having problems because in view specs the method :url_for called from will_paginate view helper in index view does not know how to gernerate the url for paginator. It seems there is some kind of problem with nested resources and routes when invoked from rspec spec views.

This could be a bug. Im not really sure yet.

Anyway i already found a solution that works for me:

I stub the method (:url_for) for index view specs, only for nested resources using will_paginate

allow(view).to receive(:url_for).and_return('#') 

Example of index view spec for a nested resource. I hope helps anyone.

require 'rails_helper'
require 'will_paginate/array'

RSpec.describe "tricks/index", :type => :view do

  context "renders a list of tricks with pagination " do

    before(:each) do
      @sport      = FactoryGirl.create(:sport)  
      @user       = FactoryGirl.create(:user)
      @trick_kind = FactoryGirl.create(:trick_kind, sport: @sport)
      @paginate = 4
      @page = 2
      FactoryGirl.create_list(:trick, @paginate + 1,  sport: @sport, user: @user, trick_kind: @trick_kind )
      @tricks = @sport.tricks.paginate(:page => @page, :per_page => @paginate)
      allow(view).to receive(:url_for).and_return('#')

    it " should display data" do
      @tricks.each do |trick|   
        assert_select "span.trick_sport_name", :text =>
        assert_select "span.trick_user_nickname", :text => trick.user.nickname
        assert_select "span.trick_sport_name", :text =>
        assert_select "span.trick_votes_count", :text => trick.votes_for.size

    it " should display last page with one element" do    
      assert_select ".trick", :count => 1
      assert_select ".trick_name", :count => 1
      assert_select "iframe", :count => 1
      assert_select ".trick_user_nickname", :count => 1
      assert_select ".trick_votes_count", :count => 1  

    it " should display pagination" do
      assert_select ".pagination", :count => 1

    it " should have to be active the @page item" do
      assert_select "", :count => 1
      assert_select "", :text => @page



Thats about the same as my specs. I vaguely remember digging into this and the cause seems to be that RSpec view specs are a thin veneer over ActionView::Testcase (or something similar) which doesn't set up the context for url_for properly. That's why feature specs exercising the same view work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment