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

发布于 2024-10-08 18:19:53 字数 859 浏览 0 评论 0原文

我正在尝试编写一个规范来测试命名空间的控制器,我们将其称为 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"}

对于另一个操作,我进行了基本相同的测试,并且我发现响应不成功。在实际的网络应用程序中,我可以很好地访问这些网址。我应该提到的一件事是,这不是一个 RESTful 资源(它作为一个资源没有任何意义),所以它实际上只是一组相关的管理操作,所以在我的 paths.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 带来麻烦,尤其是对于像我这样的新手。谢谢!

I;m trying to write a spec that tests a controller that is namespaced, let's call it Admin::FooController. My spec is called admin_foo_controller_spec.rb. In the spec, I'm trying to write a very basic spec to see if it can retrieve an action of a controller.

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

But instead I get an error:

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

For another action, I have basically the same test, and I get that the response is not successful. In the actual webapp, I can access these urls fine. One thing I should mention is that this isn't a RESTful resource (it makes no sense as one) so it's really just a group of related administrative actions, so in my routes.rb it's basically like

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

Any ideas what I'm doing wrong? It seems like namespacing is asking for trouble with rails, especially for a novice like me. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-10-15 18:19:53

在您的路线中,您的 Foo 控制器中没有索引操作,请尝试获取“设置”。

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

在控制器规范中,您需要定义控制器的操作而不是真正的路线。您在集成测试中尝试这些路线。

In your route your have no index action in you Foo controller try to get 'settings'

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

In a controller spec you need define the action of your controller not the real route. You try the routes in a integration test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文