2014-03-04 137 views
1

我有一個Admin模型,它可以管理Organization s。 我有一個AdminController與一個簡單的index行動和一個孩子Admin::OrganizationsController控制器,我試圖測試。測試導軌嵌套控制器

測試這個孩子控制器上的規範show動作傳遞沒有錯誤:

describe "GET show" do 
    it "assigns the requested organization as @organization" do 
    org = FactoryGirl.create(:organization) 
    get :show, id: org.id # <---- this works 
    expect(assigns(:organization)).to eq(org) 
    end 
end 

但是當我嘗試測試destroy動作,我得到一個錯誤,我無法理解(因此解決):

describe "DELETE destroy" do 
    it "destroys the requested organization" do 
    org = FactoryGirl.create(:organization) 
    delete :destroy, id: org.id # <---- (I tried to use org.id.to_param, unsuccessfully) 
    # ...rest of the test 
    end 
end 

,錯誤:

Failure/Error: expect { delete :destroy, id: org.id }.to change(Organization, :count).by(-1) 
    NameError: 
     undefined local variable or method `organizations_url' for #<Admin::OrganizationsController:0x007fefe1622248> 

我懷疑這與我的控制器「嵌套」(它需要像admin_organizations_url我猜)。

任何幫助?

(另外一側的相關信息:Rails的4.0.1,rspec的3.0.0.beta1)

+0

爲了我自己的好奇心,Admin :: OrganizationsController是什麼樣的?我想知道你是否在控制器中引用上面的幫助器,這會導致測試炸燬... – CDub

+0

@CDub這是一個標準的腳手架控制器,帶有規範的'index','show','edit' ,「摧毀」等行爲。實際上,它的所有內容都是從腳手架的「組織控制器」中「偷走」的。 – whatyouhide

+1

這可能是你的問題,然後......如果你沒有改變控制器的腳手架,它仍然引用'organizational_url'而不是'admin_organizations_url' ... – CDub

回答

1

「靈感」 源於CDub的評論,我接過一看destroy行動Admin::OrganizationController,這是這樣的:

def destroy 
    @organization.destroy 
    respond_to do |format| 
    format.html { redirect_to organizations_url } # <--- has to be admin_organizaions_url 
    format.json { head :no_content } 
    end 
end 

我沒注意respond_to區塊根本就是