使用 RSpec 2 和 Rails3 测试嵌套命名空间控制器

发布于 2024-10-22 00:14:19 字数 1198 浏览 3 评论 0原文

我觉得在这种情况下,代码胜于雄辩,因此将代码放在:

config/routes.rb

namespace :embed do
  namespace :v1 do
    resources :articles
  end
end

app/controllers/embed/v1/articles_controller.rb

class Embed::V1::ArticlesController < ApplicationController
  def index
    render :text => 'ok'
  end
end

< strong>spec/controllers/embed/v1/articles_controller_spec.rb

require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')

describe Embed::V1::ArticlesController do
  it "should do something" do
    get :new
  end
end

运行 rspec spec

$ rspec spec
F

Failures:

  1) Embed::V1::ArticlesController should do something
     Failure/Error: get :new
     AbstractController::ActionNotFound:
       The action 'new' could not be found for Embed::V1::ArticlesController
     # ./spec/controllers/embed/v1/articles_controller_spec.rb:5

Finished in 0.01665 seconds
1 example, 1 failure

知道为什么吗?有嵌套限制吗? 访问 URL http://0.0.0.0:3000/embed/v1/articles 会按预期呈现 ok

I feel that code will speak more than words in this case, so place to The code :

config/routes.rb

namespace :embed do
  namespace :v1 do
    resources :articles
  end
end

app/controllers/embed/v1/articles_controller.rb

class Embed::V1::ArticlesController < ApplicationController
  def index
    render :text => 'ok'
  end
end

spec/controllers/embed/v1/articles_controller_spec.rb

require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')

describe Embed::V1::ArticlesController do
  it "should do something" do
    get :new
  end
end

Running rspec spec

$ rspec spec
F

Failures:

  1) Embed::V1::ArticlesController should do something
     Failure/Error: get :new
     AbstractController::ActionNotFound:
       The action 'new' could not be found for Embed::V1::ArticlesController
     # ./spec/controllers/embed/v1/articles_controller_spec.rb:5

Finished in 0.01665 seconds
1 example, 1 failure

Any idea why is that? Is there a nested limitation?
Accessing the url http://0.0.0.0:3000/embed/v1/articles renders ok as expected.

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

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

发布评论

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

评论(2

夕嗳→ 2024-10-29 00:14:19

您没有在 Embed::V1::ArticlesController 中定义 new 操作,只有 index 操作。您正在尝试使用 get :new 执行规范中的 new 操作。

You don't have a the new action defined in Embed::V1::ArticlesController, only the index action. You are trying to hit the new action in your specs with get :new.

oО清风挽发oО 2024-10-29 00:14:19

您应该定义新的操作,在您的代码中,您没有在控制器中定义新操作并在 rspec 上调用新操作!

You should define the action new, in your code you didn't defined in controller the new action and called on rspec the new action!

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