使用命名空间进行 Rails3 和 Rspec2 控制器测试

发布于 2024-12-19 13:24:03 字数 1717 浏览 2 评论 0原文

我正在尝试使用名称空间测试控制器,以下是我的控制器 (/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 技术交流群。

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

发布评论

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

评论(1

筑梦 2024-12-26 13:24:03

当您为路由命名时,您将创建如下所示的 URL 和路径帮助程序:

HTTP Verb  Path                     action   helper
GET        /admin/sites             index    admin_sites_path
GET        /admin/sites/new         new      new_admin_site_path
POST       /admin/sites             create   admin_sites_path
GET        /admin/sites/:id         show     admin_site_path(:id)
GET        /admin/sites/:id/edit    edit     edit_admin_site_path(:id)
PUT        /admin/sites/:id         update   admin_site_path(:id)
DELETE     /admin/sites/:id         destroy  admin_site_path(:id)

因此您可以直接在代码中使用它们(即 redirect_to admin_site_path(@site) ),或者您可以这样做像这样的东西:

redirect_to([:admin, @site])

When you namespace the route, you're creating URL and path helpers that look like this:

HTTP Verb  Path                     action   helper
GET        /admin/sites             index    admin_sites_path
GET        /admin/sites/new         new      new_admin_site_path
POST       /admin/sites             create   admin_sites_path
GET        /admin/sites/:id         show     admin_site_path(:id)
GET        /admin/sites/:id/edit    edit     edit_admin_site_path(:id)
PUT        /admin/sites/:id         update   admin_site_path(:id)
DELETE     /admin/sites/:id         destroy  admin_site_path(:id)

So you can either use those directly in your code (i.e. redirect_to admin_site_path(@site) ), or you can do something like:

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