Skip to content

Instantly share code, notes, and snippets.

Aaron Kromer cupakromer

Block or report user

Report or block cupakromer

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cupakromer
cupakromer / books_controller.rb
Last active Sep 22, 2019
Rails 4 Standard CRUD Controller
View books_controller.rb
# No flash messages are set with this controller.
#
# This is a fairly basic / bare controller which does only the basic CRUD.
class BooksController < ApplicationController
respond_to :html, :xml, :json
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
respond_with @books = Book.all
View gist:8948898
# spec/support/feature_sigin_helpers.rb
# Just some useful namespace
module KrackenFeatures
module SignInHelpers
def sign_in(person)
visit new_user_session_path
fill_in 'Email', with: person.email
fill_in 'Password', with: person.password
@cupakromer
cupakromer / gist:3371003
Created Aug 16, 2012
each_with_object vs inject
View gist:3371003
my_array = %w[test one two three]
# Inject takes the value of the block and passes it along
# This often causes un-intended errors
my_array.inject({}) do |transformed, word|
puts transformed
transformed[word] = word.capitalize
end
# Output:
# {}
@cupakromer
cupakromer / gist:974c6fb9d0d6de3c2a6e
Created Jul 15, 2014
Various way to test for monkey patching with RSpec 3's `instance_double`
View gist:974c6fb9d0d6de3c2a6e
class Calculator
def reduce(operator)
fail "You shouldn't be calling this directly!"
end
end
def uses_a_duck_type(calculation)
calculation.reduce(:+)
end
View verbose_alternative.rb
class NotesController < ApplicationController
def index
notes = requested_notes
respond_to do |format|
format.html do
render :index, locals: { notes: notes }
end
format.json do
render :index, locals: { notes: notes } if stale? notes
end
View _simple_rspec.rb
#!/usr/bin/env ruby
#encoding: UTF-8
################################################################################
################################################################################
################################################################################
# Overly simplistic implementation of how a RSpec test works #
################################################################################
View gist:8480641
describe MyClass do
context "with a barish foo" do
let(:foo) { :bar }
context "doing thing 1" do # #instance_method_1
# ...
end
context "doing thing 2" do # #instance_method_2
@cupakromer
cupakromer / gist:8318170
Last active Jan 2, 2016
Find outter most example group description
View gist:8318170
def top_level_description(e)
top_level(e)[:description]
end
def top_level_object(e)
top_level(e)[:description_args].first
end
# Finds the top level example group.
@cupakromer
cupakromer / gist:8302904
Last active Jan 2, 2016
Blog: Handling multiple examples with RSpec
View gist:8302904

Normally when testing objects with RSpec, I prefer to setup the object state in top-level as context. Inside those contexts, the behavior (messages) are then each tested inside of that state. See ("TBD FILL IN HERE")[] for more details on this.

However, there are times you find yourself needing to pass in a lot of test cases for some behavior.

@cupakromer
cupakromer / gist:8201917
Last active Jan 1, 2016
Radius iBeacon offline mode minor decoder
View gist:8201917
def decode_minor(minor)
day = minor >> 11
hour = (minor >> 6) & 0b11111
min = minor & 0b111111
"#{day} #{hour}:#{min} UTC"
end
You can’t perform that action at this time.