使用 RSpec 2 和 Rails3 测试嵌套命名空间控制器
我觉得在这种情况下,代码胜于雄辩,因此将代码放在:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在
Embed::V1::ArticlesController
中定义new
操作,只有index
操作。您正在尝试使用get :new
执行规范中的new
操作。You don't have a the
new
action defined inEmbed::V1::ArticlesController
, only theindex
action. You are trying to hit thenew
action in your specs withget :new
.您应该定义新的操作,在您的代码中,您没有在控制器中定义新操作并在 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!