0

我正在尝试构建正确的 rspec 测试来验证我的 410 状态代码处理程序是否正常工作。以下是所有路线捕获的样子:

match '*not_found', to: 'error#error_404', via: :all

我的简单错误控制器:

class ErrorController < ApplicationController

  def error_404
    head status: 410
  end

end

我目前的 rspec:

require 'spec_helper'

describe ErrorController do

  context "Method #error_404 handling missing routes =>" do
    it "Should have the 410 status code.." do
      get :error_404
      expect(response.status).to be(410)
    end
  end

end

Rspec 错误消息:

  1) ErrorController Method #error_404 handling missing routes => Should have the 410 status code..
     Failure/Error: get :error_404
     ActionController::UrlGenerationError:
       No route matches {:action=>"error_404", :controller=>"error"}
     # ./spec/controllers/error_controller_spec.rb:7:in `block (3 levels) in <top (required)>'

关于如何通过此测试的任何想法?我知道该路线将不存在,但我无法尝试使用get它来使其工作......

4

1 回答 1

0

我想知道这里是否有人有更好的主意.....但是,这是我解决它的方法:

首先我安装了capybara

然后我将路线调整为更具描述性:

match '*not_found', to: 'error#error_status_410', via: :all

然后我调整了我的错误控制器:

class ErrorController < ApplicationController

  def error_status_410
    head status: 410
  end

end

最后我调整了我的错误控制器规范:

require 'spec_helper'

describe ErrorController do

  context "Method #error_status_410 handling missing routes =>" do
    it "Should have the 410 status code.." do
      visit '/will-never-be-a-route'
      page.status_code == 410
    end
  end

end
于 2014-06-13T04:36:58.460 回答