Skip to content

Instantly share code, notes, and snippets.

@odlp
odlp / hit.rb
Last active January 21, 2021 17:57
Hit a spec
# spec/support/hit.rb
module HitSpec
def hit(*args, &block)
n = 100
if args.last.is_a?(Hash) && args.last.key?(:n)
n = args.last.delete(:n)
end
@odlp
odlp / cache_spec.rb
Created April 10, 2020 18:25
Test caching mechanism
it "doesn't make further requests when the cache is warm" do
stub = stub_request(:get, "www.amazon.com")
Amazon::API.new.item_prices("id")
Amazon::API.new.item_prices("id")
# https://github.com/bblimke/webmock#setting-expectations-in-rspec-on-the-stub
expect(stub).to have_been_requested.once
end
@odlp
odlp / has_string_enum.rb
Last active June 14, 2019 14:41
Rails shorthand string enum values
# frozen_string_literal: true
# app/models/concerns
module HasStringEnum
extend ActiveSupport::Concern
class_methods do
def string_enum(name, values, **options)
enum(name => values.zip(values).to_h, **options)
end
@odlp
odlp / 1_pg_enum_migrator.rb
Last active June 14, 2019 15:07
Postgresql Enum Migrator - from integers to PG enum values
# frozen_string_literal: true
class PgEnumMigrator
def initialize(migration:, table:, column:, enum_name:, mapping:, new_default: nil, old_default: nil)
@migration = migration
@table = table
@column = column
@enum_name = enum_name
@mapping = mapping
@new_default = new_default
@odlp
odlp / table_support.rb
Last active August 13, 2020 14:37
Capybara table helper
module TableSupport
def column_names(table_selector = "table")
find(table_selector).all("th").map(&:text)
end
def values_for_column(column_name, table_selector = "table")
table = find(table_selector)
index = table.all("th").map(&:text).find_index(column_name)
expect(index).to be_present, "Unable to find column named: #{column_name}"
@odlp
odlp / bullet_example.rb
Last active January 2, 2019 10:58
Conditional Bullet in RSpec tests
# Make Bullet failing tests which opt-in to 'strict' mode
#
# Based on:
# https://github.com/flyerhzm/bullet#run-in-tests
# spec/rails_helper.rb or similar
RSpec.configure do |config|
config.before(:example, :ar_strict) do
Bullet.enable = true
Bullet.bullet_logger = true
@odlp
odlp / bundler-rspec-inline.rb
Created August 9, 2018 09:43
Inline Bundler and autorun RSpec
require "bundler/inline"
gemfile do
gem "rspec"
end
require "rspec/autorun"
RSpec.describe "inline Bundler and autorun RSpec" do
it "is convenient for self-contained examples & bug repros" do
@odlp
odlp / check_test_filenames.sh
Last active June 4, 2018 09:34
Check test / spec files are named correctly
#!/bin/sh
# Checks test files are named "_spec.rb" to match the RSpec globs property
# defined in .circleci/config.yml. This will fail the build if there are test
# files which don't match the required pattern.
# Ignores first-level helper files (e.g. spec_helper.rb) and the spec/support,
# spec/factories, spec/shared_examples folders
set -eu
@odlp
odlp / percentage_error.sh
Last active May 19, 2021 14:57
Percentage error output in tests
#!/bin/bash
# Save stdout & stderr to individual files:
bundle exec rspec > >(tee stdout.log) 2> >(tee stderr.log >&2)
# Calculate the percentage:
wc -l stderr.log stdout.log \
| ruby -e \
'err, _, total = ARGF.map(&:to_f); puts "#{(err/total*100).round}% error output"'
@odlp
odlp / .gitattributes
Created March 5, 2018 14:35
Boss, it's 100% Go!
**/* linguist-language=Go