测试Rails嵌套控制器

时间:2014-03-04 23:10:02

标签: ruby-on-rails rspec ruby-on-rails-4 functional-testing

我有Admin模型可以管理Organization。 我有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)

1 个答案:

答案 0 :(得分: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阻止