【问题标题】:rails 4 api rspec test http status code 410rails 4 api rspec测试http状态码410
【发布时间】:2014-08-03 13:27:59
【问题描述】:

我正在尝试构建正确的 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 使其工作......

【问题讨论】:

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


    【解决方案1】:

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

    首先我安装了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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      相关资源
      最近更新 更多