ruby on rails - How do I share code across a test run in rspec? -


i'm writing tests api. output request, params , other such data file when the tests run. have added log call in each of test methods calls utilites.rb file in spec/support. works expected except utilities loaded on each individual test can't write file how want to.

here spec_helper.rb

# file copied spec/ when 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 custom matchers , macros, etc, # in spec/support/ , subdirectories. dir[rails.root.join("spec/support/**/*.rb")].each { |f| require f }  rspec.configure |config|   # ## mock framework   #   # if prefer use mocha, flexmock or rr, uncomment appropriate line:   #   # config.mock_with :mocha   # config.mock_with :flexmock   # config.mock_with :rr    # remove 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 run each of   # examples within transaction, remove following line or assign false   # instead of true.   config.use_transactional_fixtures = true    # if true, base class of anonymous controllers inferred   # automatically. default behavior in future versions of   # rspec-rails.   config.infer_base_class_for_anonymous_controllers = false    # run specs in random order surface order dependencies. if find   # order dependency , want debug it, can fix order providing   # seed, 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') |f|         puts input         f.puts input       end     end end 

as can see files append test_output.txt after running each test file cleared between running rspec spec/ each time. how can make work way want to?

first of all, don't think idea. if want debug application, can read more here: http://nofail.de/2013/10/debugging-rails-applications-in-development/

if really have it, this:

assuming doing in controller tests, add spec_helper.rb:

  config.before(:all, type: :controller)     clear_log_file   end    config.after(:each, type: :controller)     log_request   end 

where clear_log_file , log_request refer code use according utilities module.

this way won't have add specs write log.

i'm 90% sure works, @ least it's general direction of should do.


Comments