[英]Mixed up with rspec assigns while testing controller
我正在嘗試了解測試控制器,到目前為止,我似乎仍停留在最簡單的問題上。
documents.controller:
def edit
@document = Document.find(params[:id])
end
documents_controller_spec:
describe 'GET #edit', focus: true do
before(:each) { @doc = FactoryGirl.create(:document)}
it "should assign @document to the document" do
get :edit, id: @doc
assigns(:document).should eq(@doc)
end
end
始終返回false。 @document始終分配為nil。 我嘗試將params [id]指定為@ doc.id,但這並不能解決任何問題。 我在這里做錯了什么?
從rspec的角度看,您正在一行中比較兩個文檔(分配給@doc):
assigns(:document).should eq(@doc)
是不一樣的-它們在數據庫中具有相同的ID,但是您要比較的實際紅寶石對象是不同的,因為其中一個是在“之前”塊中創建的,而另一個可能是在GET請求執行時在控制器內部實例化的在您的測試中。
我的猜測是以下將起作用:
assigns(:document).id.should eq(@doc.id)
嘗試改變
get :edit, id: @doc
至
get :edit, id: @doc.id
基本上,它只需要ID即可構建路線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.