Rails 3和Rspec 2命名控制器问题

时间:2010-12-24 06:54:21

标签: ruby-on-rails namespaces rspec rspec2

我试图编写一个测试命名空间控制器的规范,让我们称之为Admin::FooController。我的规范称为admin_foo_controller_spec.rb。在规范中,我正在尝试编写一个非常基本的规范,看看它是否可以检索控制器的动作。

describe "GET 'index'" do
  it "should be successful" do
    get 'index'
    response.should be_success
  end
end

但我得到一个错误:

 Failure/Error: get 'index'
 No route matches {:controller=>"admin/foo"}

对于另一个动作,我的测试基本相同,并且我得知响应不成功。在实际的webapp中,我可以很好地访问这些网址。我应该提到的一点是,这不是RESTful资源(它没有任何意义)所以它实际上只是一组相关的管理操作,所以在我的routes.rb中它基本上就像

namespace :admin do
  scope "foo" do
    match "action1" => "foo#action1"
    match "action2" => "foo#action2"
    match "/index" => "foo#settings"
    match "settings" => "foo#settings"
  end
end

任何想法我做错了什么?似乎命名空间要求使用rails的麻烦,特别是像我这样的新手。谢谢!

1 个答案:

答案 0 :(得分:2)

在你的路线中你没有索引动作你Foo控制器试图获得'设置'

describe "GET 'index'" do
  it "should be successful" do
    get 'settings'
    response.should be_success
  end
end

在控制器规范中,您需要定义控制器的操作而不是实际路径。您在集成测试中尝试路由。