使用命名空间进行 Rails3 和 Rspec2 控制器测试
我正在尝试使用名称空间测试控制器,以下是我的控制器 (/admin/sites_controller.rb):
class Admin::SitesController < AdminController
def create
@site = Site.new(params[:site])
respond_to do |format|
if @site.save
format.html { redirect_to(@site, :notice => 'Site was successfully created.') }
format.xml { render :xml => @site, :status => :created, :location => @site }
else
format.html { render :action => "new" }
format.xml { render :xml => @site.errors, :status => :unprocessable_entity }
end
end
end
end
以下是我的 routes.rb
文件
namespace :admin do
resources :sites
end
我正在使用 rspec2 来测试我的控制器以下是我的控制器规范
describe Admin::SitesController do
describe "POST create" do
describe "with valid params" do
it "creates a new Site" do
expect {
post :create, :site => valid_attributes
}.to change(Site, :count).by(1)
end
end
end
end
,但是当我运行该规范时,它给出了以下路由错误
Admin::SitesController POST create with valid params creates a new Site
Failure/Error: post :create, :site => valid_attributes
NoMethodError:
undefined method `site_url' for #<Admin::SitesController:0xb5fbe6d0>
# ./app/controllers/admin/sites_controller.rb:47:in `create'
# ./app/controllers/admin/sites_controller.rb:45:in `create'
# ./spec/controllers/admin/sites_controller_spec.rb:78
# ./spec/controllers/admin/sites_controller_spec.rb:77
,我猜这是因为我正在使用的“admin”命名空间,但我该如何解决这个问题?
我正在使用
- Rails3
- Rspec2
- Linux
I'm trying to test a controller with a name space, following is my controller (/admin/sites_controller.rb):
class Admin::SitesController < AdminController
def create
@site = Site.new(params[:site])
respond_to do |format|
if @site.save
format.html { redirect_to(@site, :notice => 'Site was successfully created.') }
format.xml { render :xml => @site, :status => :created, :location => @site }
else
format.html { render :action => "new" }
format.xml { render :xml => @site.errors, :status => :unprocessable_entity }
end
end
end
end
and following is my routes.rb
file
namespace :admin do
resources :sites
end
I'm using rspec2 to test my controller and following is my controller spec
describe Admin::SitesController do
describe "POST create" do
describe "with valid params" do
it "creates a new Site" do
expect {
post :create, :site => valid_attributes
}.to change(Site, :count).by(1)
end
end
end
end
But when I run the spec it gives me the following routing error
Admin::SitesController POST create with valid params creates a new Site
Failure/Error: post :create, :site => valid_attributes
NoMethodError:
undefined method `site_url' for #<Admin::SitesController:0xb5fbe6d0>
# ./app/controllers/admin/sites_controller.rb:47:in `create'
# ./app/controllers/admin/sites_controller.rb:45:in `create'
# ./spec/controllers/admin/sites_controller_spec.rb:78
# ./spec/controllers/admin/sites_controller_spec.rb:77
I guess its because of the 'admin' name space I'm using, but how can I fix that?
I'm using
- Rails3
- Rspec2
- Linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您为路由命名时,您将创建如下所示的 URL 和路径帮助程序:
因此您可以直接在代码中使用它们(即
redirect_to admin_site_path(@site)
),或者您可以这样做像这样的东西:When you namespace the route, you're creating URL and path helpers that look like this:
So you can either use those directly in your code (i.e.
redirect_to admin_site_path(@site)
), or you can do something like: