Skip to content

Instantly share code, notes, and snippets.

View ClayShentrup's full-sized avatar
🏗️
🗳️🌱🌷🌐🏗️🏓☢️

Clay Shentrup ClayShentrup

🏗️
🗳️🌱🌷🌐🏗️🏓☢️
View GitHub Profile
@ClayShentrup
ClayShentrup / shock.rb
Last active December 29, 2017 22:32
Sandi Metz COC
# Source: https://www.youtube.com/watch?v=f5I1iyso29U&t=23m44s
class Shock
def cost(type)
case type
when :front
FrontShockCost.new.compute
when :rear
RearShockCost.new.compute
when :lefty
LeftyShockCost.new.compute
@ClayShentrup
ClayShentrup / ajax.js
Last active September 20, 2016 17:53
Jasmine spec helper basics
App.testHelpers.stubAjaxRequestWithData = function (requestURL, data, status) {
jasmine.Ajax.stubRequest(requestURL)
.andReturn({
status: status,
responseText: data,
contentType: 'text/html',
});
};
App.testHelpers.stubSuccessfulAjaxRequestWithData =
@ClayShentrup
ClayShentrup / vcr_setup.rb
Created August 11, 2016 23:05
vcr_setup.rb
# frozen_string_literal: true
require('vcr')
VCR.configure do |config|
config.cassette_library_dir = 'spec/cassettes'
config.hook_into :webmock
config.configure_rspec_metadata!
config.ignore_localhost = true
# Don't change this here! Instead, to record an initial cassette,
@ClayShentrup
ClayShentrup / mock_clock.js
Created August 2, 2016 04:23
Stubbing Jasmine time
'use strict';
beforeEach(function () {
jasmine.clock().install();
jasmine.clock().mockDate(
new Date(2015, 4, 20)
);
});
afterEach(function () {
RSpec.shared_examples('a foreign_key constraint association') do
specify do
expect do
build(described_model, "#{association}_id" => 0).save(validate: false)
end.to raise_error(ActiveRecord::InvalidForeignKey)
end
end
@ClayShentrup
ClayShentrup / names_for_let_object.rb
Last active June 23, 2016 00:51
names_for_let_objects 2
module NamesForLetObject
def names_for_let_objects(objects)
objects.map { |object| name_for_let_object(object) }
end
def name_for_let_object(object)
__memoized.instance_variable_get(:@memoized).key(object)
end
end
expected collection contained: [#<User id: 1, email: "user1@example.com", ...a bunch of attributes...>]
actual collection contained: [#<User id: 1, email: "user1@example.com", ...a bunch of attributes...>,
#<User id: 2, email: "user2@example.com", ...a bunch of attributes...>]
@ClayShentrup
ClayShentrup / car.rb
Last active April 22, 2017 17:45
Skipping ActiveRecord association validations in tests
class Car < ActiveRecord::Base
attr_accessor(:skip_association_presence_validations)
validates(:driver, presence: true, unless: :skip_association_presence_validations)
# creates driver_license_number method
delegate(:license_number, to: :driver, prefix: true)
end
@ClayShentrup
ClayShentrup / spec_example.rb
Created December 2, 2015 20:48
Testing that code executes within a block, e.g. transaction or time zone
it('uses the client time zone') do
allow(described_class).to receive(:delete_cache) do
fail unless Time.zone == client_time_zone
end
allow(described_class).to receive(:create_cache) do
fail unless Time.zone == client_time_zone
end
update_for_year
end
@ClayShentrup
ClayShentrup / original.rb
Created November 16, 2013 20:25
Trying to understand the point of Gary Bernhardt's "Boundaries" talk
describe Sweeper do
context 'a subscription is expired' do
let(:bob) { double(active?: true, paid_at: 2.months.ago) }
let(:users) { [bob] }
before { allow(User).to receive(:all).and_return(users) }
it 'emails the user' do
expect(UserMailer).to receive(:billing_problem).with(bob)
described_class.sweep
end