簡體   English   中英

Rspec視圖因可安裝Rails 4的引擎而失敗

[英]Rspec views fail with Rails 4 mountable engine

我已經創建了一個名為Foobar的可安裝Rails 4引擎的Rails 4應用程序。 該引擎包含用於Items的支架。

在引擎中設置rspec-rails后,我無法通過視圖規格。

我收到以下錯誤:

2) foobar/items/index renders a list of items
   Failure/Error: render
   ActionView::Template::Error:
     undefined method `item_path' for #<#<Class:0x007fe6dae7b3c8>:0x007fe6da9a2ec8>
   # ./app/views/foobar/items/index.html.erb:21:in `block in ___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
   # ./app/views/foobar/items/index.html.erb:16:in `each'
   # ./app/views/foobar/items/index.html.erb:16:in `___sers_ianneub__ocuments__ode_rails_engine_spec_test_engines_foobar_app_views_foobar_items_index_html_erb__1719273292435665376_70314743713380'
   # ./spec/views/foobar/items/index.html.erb_spec.rb:20:in `block (2 levels) in <top (required)>'

在外部應用程序或引擎的規范/虛擬應用程序中啟動Rails服務器時,所有視圖似乎都可以正常工作。

所以我的問題是:

即使引擎視圖在Rails服務器上運行正常,為什么rspec測試仍然失敗?

獎勵問題:

我可以在index.html.erb中進行哪些更改以使測試通過?

我真的想了解rspec測試中的作用。 恕我直言,rspec(和/或rspec-rails)似乎有一個問題,阻止了此工作。 就像測試中沒有加載某些東西一樣,這些東西通常會使它在rails服務器中工作。 換句話說:如果它在rails服務器中工作,那么它應該在rspec中以相同的方式工作。 正確嗎? 我想念什么?

該應用程序位於此處: https : //github.com/ianneub/rails-engine-spec-test

您應該能夠克隆存儲庫並運行以下命令來復制錯誤:

  1. cd engines/foobar
  2. bundle install
  3. rspec

預先感謝您與我(以及全世界)分享的任何幫助和指導。

“ index.html.erb”視圖中的第21行需要范圍確定為foobar gem。

<td><%= link_to 'Show', foobar.item_path(item) %></td>

這是因為Rspec渲染器不包含url_helpers方法,而rails渲染器以某種方式包括了這些方法。 可以用前鈎固定:

RSpec.configure do |config|
  config.before(:example, type: :view) do
    view.class_eval do
      include <Your Engine Namespace>::Engine.routes.url_helpers
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM