Skip to content

Instantly share code, notes, and snippets.

@westonganger
Last active March 26, 2023 03:23
Show Gist options
  • Select an option

  • Save westonganger/f2240a0d1031406e8b97f17b698f1eec to your computer and use it in GitHub Desktop.

Select an option

Save westonganger/f2240a0d1031406e8b97f17b698f1eec to your computer and use it in GitHub Desktop.
Rails Spec Helpers
def sample_pdf_upload
sample_path = Rails.root.join("spec/fixtures/sample_file.pdf")
#fixture_file_upload(sample_path, 'application/pdf') ### MINITEST
Rack::Test::UploadedFile.new(sample_path) ### RSPEC
end
def sample_csv_upload
sample_path = Rails.root.join("spec/fixtures/sample_file.csv")
#fixture_file_upload(sample_path, 'text/csv') ### MINITEST
Rack::Test::UploadedFile.new(sample_path) ### RSPEC
end
def last_email
ActionMailer::Base.deliveries.last
end
### VERY USEFUL FOR INSPECTING REQUESTS WITHIN SYSTEM/FEATURE SPECS
def inspect_requests(inject_headers: {})
Testing::RequestInspectorMiddleware.log_requests!(inject_headers)
yield
Testing::RequestInspectorMiddleware.requests
ensure
Testing::RequestInspectorMiddleware.stop_logging!
end
def append_error_message(msg, &block)
exceptions = [
RSpec::Expectations::ExpectationNotMetError,
Capybara::ElementNotFound,
Minitest::Assertion,
]
begin
block.call
rescue *exceptions => e
e.message << "\n#{msg}"
raise e
end
end
def within_subdomain(subdomain)
### FOR REQUEST SPECS
orig_host = host
host!("#{subdomain}.#{orig_host}")
yield
host!(orig_host)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment