如何在rspec中的测试运行中共享代码?

时间:2014-03-10 22:36:59

标签: ruby-on-rails ruby rspec

我正在为我的API编写测试。我想在运行测试时将请求,参数和其他此类数据输出到文件中。我在每个测试方法中添加了一个日志调用,调用spec / support中的utilites.rb文件。这可以正常工作,除了在每个单独的测试中加载实用程序,所以我不能写我想要的文件。

这是我的spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../conf
require 'rspec/autorun'ig/environment", __FILE__)
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"

  config.include Requests::JsonHelpers, type: :request
  config.include Utilities
end

Utilities.rb

module Utilities
  EQUAL_STRING = "=========================\n"
  TEST_OUTPUT = "test_output.txt"

  def log
    s = "#{request.method} #{request.path}\n" +
        "Controller: #{request.params[:controller]}\n" +
        "Action: #{request.params[:action]}\n" +
        "Params:\n" +
        JSON.pretty_generate(JSON.parse(request.query_parameters.to_json)) + "\n" +
        "\n" +
        "Response:\n" +
        "Status: #{response.status}\n" +
        JSON.pretty_generate(JSON.parse(response.body)) + "\n" +
        EQUAL_STRING

    write_to_file s
  end

  private

    def write_to_file(input)
      if @file.nil?
        @file = TEST_OUTPUT
        if File.exists? @file
          File.delete @file
        end
      end

      File.open(@file, 'a') do |f|
        puts input
        f.puts input
      end
    end
end

正如您在运行每个测试后我想要附加到test_output.txt的文件中所看到的那样,但我希望每次运行rspec spec/之间清除该文件。我怎样才能按照我想要的方式完成这项工作?

2 个答案:

答案 0 :(得分:1)

首先,我认为这不是一个好主意。如果您想调试您的应用程序,可以在此处阅读更多相关信息:http://nofail.de/2013/10/debugging-rails-applications-in-development/

如果你真的真的必须这样做,那就这样做:

假设您在控制器测试中执行此操作,请将此类内容添加到spec_helper.rb

  config.before(:all, type: :controller) do
    clear_log_file
  end

  config.after(:each, type: :controller) do
    log_request
  end

其中clear_log_filelog_request是指您根据Utilities模块使用的代码。

这样您甚至不需要在规范中添加任何内容来编写日志。

我只有90%确定这是有效的,但至少这是你应该做的一般方向。

答案 1 :(得分:1)

我必须添加到规范助手。

config.before(:suite) do
  #code I added
end