RSpec将索引中的操作重新解释为在视图测试中显示

时间:2015-08-09 15:28:33

标签: ruby-on-rails ruby testing rspec

我的索引视图出现以下错误:

  1) events/index.html.erb should display all the events in a matrix formation
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:action=>"show", :controller=>"events", :format=>nil, :id=>nil, :locale=>#<Event id: 2, name: "Miles Eichmann", description: "[\"Voluptas quia velit non est.\", \"Commodi aperiam p...", image: "event.jpg", location: nil, created_at: "2015-08-09 13:55:02", updated_at: "2015-08-09 13:55:02", whenwhen: "2015-09-07 04:00:00", speaker_id: 1, map_address: "800 Rene Levesque, Montreal, H3G 5K4", lon: 0.0, lat: 0.0, price: 3550>} missing required keys: [:id]

spec / views / events / index.html.erb_spec.rb中的测试:

require 'rails_helper'

RSpec.describe "events/index.html.erb", :type => :view do
  let!(:speaker) { FactoryGirl.create(:speaker) }
  let!(:events) { FactoryGirl.create_list(:event, 10, speaker_id: speaker.id ) }

  it "should display all the events in a matrix" do
    assign(:events, events)
    render
    puts response
  end
end

我不明白为什么如果我将所有操作都指向index,则错误指向:action=>"show"

如果我删除了两个let语句或从中获取!,则测试通过并显示索引页面的标题和标题(当然没有事件)。

index.html.erb调用辅助函数以在矩阵中显示事件,如下所示:

<!-- app/views/events/index.html.erb -->
...
<% if Event.future.count == 0 %>
  <br><%= t(:staytuned) %>
<% else %>
  <h2><%= t(:futureevents).mb_chars.upcase %></h2>
  <%= raw(display_matrix_events(Event.future)) %>
<% end %>
...

这是应该调用的代码段之一,因为工厂将来会创建事件:

#spec/facories/event.rb
require 'faker'

FactoryGirl.define do
  factory :event do
    ....
    the_date { Faker::Date.forward(30) }
    ....
  end
end

在浏览器上运行时,代码运行正常。

这是堆栈跟踪:

Failures:

  1) events/index.html.erb should display all the events in a matrix
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:action=>"show", :controller=>"events", :format=>nil, :id=>nil, :locale=>#<Event id: 1, name: "Columbus Goodwin", description: "[\"Quam est officia adipisci.\", \"Molestiae cumque vo...", image: "event.jpg", location: nil, created_at: "2015-08-09 20:22:27", updated_at: "2015-08-09 20:22:27", whenwhen: "2015-09-08 04:00:00", speaker_id: 1, map_address: "800 Rene Levesque, Montreal, H3G 5K4", lon: 0.0, lat: 0.0, price: 3550>} missing required keys: [:id]
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/journey/formatter.rb:39:in `generate'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:595:in `generate'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:625:in `generate'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:661:in `url_for'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/url_for.rb:155:in `url_for'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/routing_url_for.rb:83:in `url_for'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:228:in `call'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:268:in `block (2 levels) in define_url_helper'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/routing_url_for.rb:87:in `url_for'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/url_helper.rb:180:in `link_to'
     # ./app/views/events/_speaker_in_event_index.html.erb:6:in `_app_views_events__speaker_in_event_index_html_erb__148285862205269253_56219800'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:278:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:47:in `render_partial'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/rendering_helper.rb:27:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:198:in `render'
     # ./app/helpers/events_helper.rb:21:in `display_matrix_events'
     # ./app/views/events/index.html.erb:18:in `_app_views_events_index_html_erb__3520103631403717635_53363480'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:47:in `render_template'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:17:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:42:in `render_template'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:23:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/rendering_helper.rb:24:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:198:in `render'
     # /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:118:in `render'
     # ./spec/views/events/index.html.erb_spec.rb:28:in `block (2 levels) in <top (required)>'

Finished in 2.11 seconds (files took 3.79 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/views/events/index.html.erb_spec.rb:10 # events/index.html.erb should display all the events in a matrix formation

1 个答案:

答案 0 :(得分:0)

操作index已正确执行,但它有一系列rendermethod次调用,导致show操作。

app/views/events/index.html.erb文件调用方法,该方法呈现包含行

的部分
<%= link_to image_tag(e.speaker.image.to_s, width: '150%'), e %>

RSpec按原样解释e中的link_to(即事件对象),而不是像Rails那样使用其id来构建实际链接。

e更改为e.id会引发错误:

undefined method `model_name' for Fixnum:Class

在浏览器和RSpec中。

删除此行会使测试通过,但当然会输出错误的结果。