Ruby is expecting a Hash, but you forgot something (maybe a key name?)
Example:
# wrong
foo: { # Start of the new hash
:bar # You're missing your key OR value
}| # Illustration of a neat thing that footguns me 1x a year | |
| class Foo | |
| attr_accessor :params | |
| attr_reader :val1, :val2, :val3 | |
| def initialize(params: {}) | |
| @params = params.with_indifferent_access | |
| # The below should either be: | |
| # |
| # A lot of times, dates will be handled like this: | |
| # | |
| # parsed_date = Date.parse(maybe_date) rescue nil | |
| # | |
| # This is an alternative, faster strategy that doesn't rely on a catch-all, less performant | |
| # (in error conditions at at least; backtraces aren't generated), rescue behavior | |
| def parse_date_string(maybe_date) | |
| return if maybe_date.blank? | |
| catch(:date) do |
| --- | |
| # NOTE: This was, for some reason, way too hard for me to piece together quickly | |
| services: | |
| opensearch: | |
| container_name: opensearch | |
| image: opensearchproject/opensearch:latest | |
| environment: | |
| - node.name=opensearch | |
| - cluster.name=opensearch-docker-cluster |
| # frozen_string_literal: true | |
| # Brief overview of ActiveRecord preload, eager_load, includes, and joins | |
| # TODO: Add a joins/eager_load example | |
| # TODO: Add actual SQL output for every single one of these | |
| # id | |
| class Foo < ApplicationRecord |
| # frozen_string_literal: true | |
| # This custom matcher is designed to make record changes easy | |
| # | |
| # Without this matcher you'd typically do one of: | |
| # | |
| # # I believe these won't always work, depends on object reloading, what is changed, how it's changed, | |
| # # the order things got loaded, etc. | |
| # expect { update }.to change(foo, :method)... | |
| # expect { update }.to change { foo.method }... |
| RSpec.describe MyClass do | |
| let(:my_value) { 1 } | |
| let(:other_value) { 2 } | |
| it 'does a thing' do | |
| expect(my_value).to eq(other_value) # failure condition, something flakey though | |
| rescue RSpec::Expectations::ExpectationNotMetError | |
| $ERROR_INFO.message << <<~MSG |
Ruby is expecting a Hash, but you forgot something (maybe a key name?)
Example:
# wrong
foo: { # Start of the new hash
:bar # You're missing your key OR value
}| # frozen_string_literal: true | |
| # 1. Put this in your spec folder as spec/flakey_spec.rb | |
| # 2. Run it with `rspec spec/flakey_spec.rb` | |
| # | |
| # Assumptions: | |
| # | |
| # 1. You're using rails, rspec, factory_bot, faker | |
| # 2. You have a standard-ish User model (has create_at, updated_at, etc. integer ID primary key, etc) | |
| # 3. User has a last_name field/column/attribute |
| # frozen_string_literal: true | |
| # I often find myself wanting to codify relationships/associations in a factory by default, so the | |
| # factory doesn't require myself or other engineers to retain the knowledge about the complex relationships | |
| # and define those relationships in every part of the test | |
| # | |
| # In this contrived example, our models are tenant-aware via a company_id FK on each model: | |
| # - Widget belongs_to a company | |
| # - User belongs to a company | |
| # - A widget has a creator; the creator of the widget and the widget itself must belong to the same company |
| # app/models/user.rb | |
| # | |
| class User | |
| after_create :foo | |
| private | |
| def foo | |
| # ..something we don't do very often | |
| end |