Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用的测试视图将“与RSpec给出的结果一致”;“没有路线匹配”;_Ruby On Rails_Rspec_Ruby On Rails 4_Rspec2_Will Paginate - Fatal编程技术网

Ruby on rails 使用的测试视图将“与RSpec给出的结果一致”;“没有路线匹配”;

Ruby on rails 使用的测试视图将“与RSpec给出的结果一致”;“没有路线匹配”;,ruby-on-rails,rspec,ruby-on-rails-4,rspec2,will-paginate,Ruby On Rails,Rspec,Ruby On Rails 4,Rspec2,Will Paginate,我试图测试一个使用将分页的视图,但是我得到了一个无法解释的无路由匹配的错误。请注意,通过浏览器或控制器/功能规格访问站点/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",

我试图测试一个使用
将分页的视图,但是我得到了一个无法解释的
无路由匹配的错误。请注意,通过浏览器或控制器/功能规格访问
站点/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>
给出以下错误三次:
1)站点/措施
失败/错误:渲染
ActionView::模板::错误:
没有路由匹配{:page=>2,:controller=>“stations”,:action=>“measures”}
#./app/views/stations/measures.html.erb:6:in`` app\u views\u stations\u measures\u html\u erb\u 11735444536802428572\u 7022507801400'
#./spec/views/stations/measures.html.erb_spec.rb:23:in'block(2层)in'
#./spec/views/stations/measures.html.erb_spec.rb:27:in'block(2层)in'
#-e:1:in`'
...

原因是您需要向路线提供您在routes.rb中定义的
站点id
。在所示的参数中没有这样的东西

我从未在Rspec rails中编写过视图测试,也不知道如何适应这种方式。我建议您改为使用控制器测试,添加
render\u views
宏以允许视图渲染,然后访问
response.body


由于分页逻辑主要是在controller中实现的,所以我认为在controller中对该测试进行分类是完全合理的

这不是重复的,因为该问题涉及请求规范而不是视图规范。我有一个控制器规范,用于测试它是否对
@measures
资源进行分页。我知道有些人不使用视图测试,但我发现在视图测试中删除数据库并专注于视图逻辑要容易得多。@papirtiger,我同意视图测试在需要时是完全可以接受的。但是似乎很难为这个视图测试设置正确的路径。
    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>'


    ...