Skip to content

Instantly share code, notes, and snippets.

Avatar
🏗️
🗳️🌱🌷🌐🏗️🏓☢️

Clay Shentrup ClayShentrup

🏗️
🗳️🌱🌷🌐🏗️🏓☢️
View GitHub Profile
@ClayShentrup
ClayShentrup / shock.rb
Last active Dec 29, 2017
Sandi Metz COC
View shock.rb
# 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 Sep 20, 2016
Jasmine spec helper basics
View ajax.js
App.testHelpers.stubAjaxRequestWithData = function (requestURL, data, status) {
jasmine.Ajax.stubRequest(requestURL)
.andReturn({
status: status,
responseText: data,
contentType: 'text/html',
});
};
App.testHelpers.stubSuccessfulAjaxRequestWithData =
View 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 Aug 2, 2016
Stubbing Jasmine time
View mock_clock.js
'use strict';
beforeEach(function () {
jasmine.clock().install();
jasmine.clock().mockDate(
new Date(2015, 4, 20)
);
});
afterEach(function () {
View a_foreign_key_constraint_association.rb
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 Jun 23, 2016
names_for_let_objects 2
View names_for_let_object.rb
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
View results.txt
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 Apr 22, 2017
Skipping ActiveRecord association validations in tests
View car.rb
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 Dec 2, 2015
Testing that code executes within a block, e.g. transaction or time zone
View spec_example.rb
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 Nov 16, 2013
Trying to understand the point of Gary Bernhardt's "Boundaries" talk
View original.rb
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