rails 4 api rspec test http状态码410

时间:2014-06-13 04:20:46

标签: ruby-on-rails api rspec http-status-code-410

我正在尝试构建正确的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使其工作时遇到了麻烦......

1 个答案:

答案 0 :(得分: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