如何在Rspec中输出响应体以及描述

时间:2015-12-30 10:39:12

标签: ruby-on-rails rspec

我想使用Rspec的HTML格式化程序来生成API文档,为此我认为我需要在HTML格式化程序中进行更改,我可以轻松地通过超越example_passed函数来进行更改但是问题是通过RSpec将测试的响应转换为该函数::核心:例如,我尝试按照此示例http://blog.carbonfive.com/2011/04/21/generating-documentation-from-specs/但我在 InstrumentSpecs

中出错
RSpec::Core::ExampleGroup::WrongScopeError: `example` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

我的 InstrumentSpecs 看起来像这样

require 'action_dispatch'
require 'action_controller'

module InstrumentSpecs
  module ::ActionController::TestCase::Behavior
    alias_method :process_without_logging, :process

    def process(action, http_method = 'GET', parameters = nil)
      version, route = described_class.to_s.match(/Api::V1::(\w+)Controller/)[1..2].collect(&:downcase)
      curl = "curl "
      # parameters.each {|key, value| curl << "-d \"#{key}=#{CGI.escape(value)}\" " } if parameters.present?
      curl << "-X #{http_method} "
      curl << "https://api.host.example/#{version}/#{route} "
      example.metadata[:curl] = curl

      response = process_without_logging(action, http_method, parameters)
      example.metadata[:response] = "#{response.status} #{response.body}"

      response
    end
  end
end

错误发生在

下面
example.metadata[:curl] = curl

我的规格看起来像这样

require 'rails_helper'

describe Api::V1::UsersController do
  render_views
  describe 'when user is logged in' do
    let(:user) {create(:user)}
    before do
      sign_in user
    end

    describe 'GET #index' do
      it 'gets index page' do
        create(:user)
        get :index, format: :json
        is_expected.to respond_with :ok
      end
    end
  end  
end

我正在使用rails 4.2.5和rspec 3.4

0 个答案:

没有答案