ruby-on-rails - 为什么我不能在 RSpec 功能测试中提高 Mongoid::Errors::DocumentNotFound?

标签 ruby-on-rails ruby-on-rails-3.1 mongoid rspec-rails

我正在使用 rspec-rails (2.8.1) 使用 mongoid (3.4.7) 对 rails 3.1 应用程序进行功能测试以实现持久性。我正在尝试以与 rspec-rails documentation 相同的方式在我的 ApplicationController 中为 Mongoid::Errors::DocumentNotFound 错误测试rescue_from对于匿名 Controller ,建议它可以完成。但是当我运行以下测试时......

require "spec_helper"

class ApplicationController < ActionController::Base

  rescue_from Mongoid::Errors::DocumentNotFound, :with => :access_denied

private

  def access_denied
    redirect_to "/401.html"
  end
end

describe ApplicationController do
  controller do
    def index
      raise Mongoid::Errors::DocumentNotFound
    end
  end

  describe "handling AccessDenied exceptions" do
    it "redirects to the /401.html page" do
      get :index
      response.should redirect_to("/401.html")
    end
  end
end

我收到以下意外错误
  1) ApplicationController handling AccessDenied exceptions redirects to the /401.html page
     Failure/Error: raise Mongoid::Errors::DocumentNotFound
     ArgumentError:
       wrong number of arguments (0 for 2)
     # ./spec/controllers/application_controller_spec.rb:18:in `exception'
     # ./spec/controllers/application_controller_spec.rb:18:in `raise'
     # ./spec/controllers/application_controller_spec.rb:18:in `index'
     # ./spec/controllers/application_controller_spec.rb:24:in `block (3 levels) in <top (required)>'

为什么?我怎样才能提出这个 mongoid 错误?

最佳答案

Mongoid的documentation for the exception显示它必须被初始化。更正后的工作代码如下:

require "spec_helper"

class SomeBogusClass; end

class ApplicationController < ActionController::Base

  rescue_from Mongoid::Errors::DocumentNotFound, :with => :access_denied

private

  def access_denied
    redirect_to "/401.html"
  end
end

describe ApplicationController do
  controller do
    def index
      raise Mongoid::Errors::DocumentNotFound.new SomeBogusClass, {}
    end
  end

  describe "handling AccessDenied exceptions" do
    it "redirects to the /401.html page" do
      get :index
      response.should redirect_to("/401.html")
    end
  end
end

关于ruby-on-rails - 为什么我不能在 RSpec 功能测试中提高 Mongoid::Errors::DocumentNotFound?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201769/

相关文章:

ruby-on-rails - 未知操作 : The action 'create' could not be found for RegistrationsController?

ruby-on-rails - mongoid - 覆盖现有字段警告

mongoid - 为什么没有 find_or_create_by! mongoid 的方法?

ruby-on-rails - 在 Mongoid 中链接 map_reduce 调用的正确方法是什么?

ruby-on-rails - Rails 删除不会破坏记录,只是刷新

ruby-on-rails - Rspec中的solr拒绝了连接

java - Java .NET与PHP Rails

ruby-on-rails - 使用带有数字范围的步长的 rails 生成选择标签

ruby-on-rails - rails : Some seeds don't appear to be saving

ruby-on-rails - 在 check_box_tag 中生成自定义属性