Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am cupakromer on github.
  • I am cupakromer (https://keybase.io/cupakromer) on keybase.
  • I have a public key ASDdHyS2jP9poEx6J_S1G7N5TLS52N-7YgxSMEbvRrOFdQo

To claim this, I am signing this object:

@cupakromer
cupakromer / books_controller.rb
Last active May 19, 2020 10:20
Rails 4 Standard CRUD Controller
# 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
@cupakromer
cupakromer / gist:3371003
Created August 16, 2012 15:23
each_with_object vs inject
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:
# {}
# 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:974c6fb9d0d6de3c2a6e
Created July 15, 2014 21:59
Various way to test for monkey patching with RSpec 3's `instance_double`
class Calculator
def reduce(operator)
fail "You shouldn't be calling this directly!"
end
end
def uses_a_duck_type(calculation)
calculation.reduce(:+)
end
@cupakromer
cupakromer / verbose_alternative.rb
Last active February 14, 2017 18:42 — forked from davetron5000/verbose.rb
DRY Madness
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
@cupakromer
cupakromer / _simple_rspec.rb
Created December 10, 2012 00:16
Simple RSpec
#!/usr/bin/env ruby
#encoding: UTF-8
################################################################################
################################################################################
################################################################################
# Overly simplistic implementation of how a RSpec test works #
################################################################################
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 January 2, 2016 14:39
Find outter most example group description
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 January 2, 2016 12:19
Blog: Handling multiple examples with RSpec

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.