【发布时间】:2014-01-22 16:13:10
【问题描述】:
我正在尝试测试使用 will_paginate 的视图,但我收到了无法解释的 No route matches 错误。请注意,通过浏览器或控制器/功能规范访问stations/1/measures/page/2 是有效的。
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>
这些规格:
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
给出以下错误三遍:
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>'
...
【问题讨论】:
-
这不是 stackoverflow.com/questions/10762896/… 的重复,因为该问题涉及请求规范而不是查看规范
标签: ruby-on-rails rspec ruby-on-rails-4 rspec2 will-paginate