我正在使用RSpec來測試我的視圖。我想測試edit
操作的視圖包含一個字段(或文本)「Director」。它在render
上窒息。這裏是我的代碼:在RSpec視圖中使用渲染測試
# spec/views/movies/edit.html.haml_spec.rb
require 'spec_helper'
describe 'movies/edit.html.haml' do
it 'displays \'director\' field on edit page' do
render
expect(rendered).to have_content("Director")
end
end
在運行這個我得到以下錯誤:
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"movies", :id=>nil}
據我瞭解,約定優於配置意味着render
不帶任何參數應該呈現的預期可視app/views/movies/edit.html.haml
我不知道知道爲什麼它正在尋找行動的途徑show
。該路線確實存在,但它與該規範無關。
我已經嘗試了一些參數permuations來render
沒有運氣:
ArgumentError:
You invoked render but did not give any of :partial, :template, :inline, :file or :text option.
OR
ActionView::MissingTemplate:
Missing partial /edit, movies/edit with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :coffee, :haml]}.
:
render :edit
render action: :edit # is this even valid?
render :action => :edit
render "edit"
render "edit.html.haml"
render action: "edit"
render action: "edit.html.haml"
render :action => "edit"
render :action => "edit.html.haml"
上述所有產生兩個錯誤之一
我不想渲染一個部分,我試圖呈現完整的視圖。我錯誤地使用了慣例,還是錯誤地使用了render
?
編輯1:我不認爲這是一個部分。有一個在app/views/movies/edit.html.haml
的看法。我可能不明白部分是什麼。
編輯2:我嘗試了new
操作的相同規範,它似乎運行正常(文本不存在時失敗,當文本存在時通過)。這是否意味着我需要加載edit
視圖作爲部分,還是我需要給它@movie
的虛擬數據?爲什麼edit
是偏?
它更可能是在您的實際編輯視圖,你這是一個鏈接到'movie'對象的顯示頁面,但該對象是零,而所以頁面不能被渲染並且給你那個錯誤。看看你是在做link_to movie還是link_to @ movie或者form_for @ movie,或者是其他地方的變化。 – MrDanA
我在視圖中有'form_tag movie_path(@movie)'。你是這個意思嗎? –
是的,這就是我的意思。你不要在任何地方設置這個變量,所以當Rails渲染那條路徑時,它會失敗,因爲它知道路徑是假的。在你的測試中,只需執行'@movie = X',然後創建一個新電影或者設置一個非零的ID屬性的假電影。另外,你的觀點不是局部的。我認爲你認爲它的唯一原因是由於ActionView :: MissingTemplate錯誤中的默認文本。別擔心。你知道這不是一個部分。 – MrDanA