Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

  • Colorado, USA
  • 00:15 (UTC -06:00)
  • LinkedIn in/jko170
View GitHub Profile
@justinko
justinko / gist:1684051
Created January 26, 2012 17:59
Methods to aid in testing without loading the Rails environment
def stub_model(model_name)
stub_class model_name, ActiveRecord::Base
end
def stub_class(class_name, super_class = Object)
stub_module class_name, Class.new(super_class)
end
def stub_module(module_name, object = Module.new)
module_name = module_name.to_s.camelize
@justinko
justinko / gist:1702877
Created January 30, 2012 06:16
Ruby 1.8 Proc equality bug
a = lambda {}
b = a.dup
m = Module.new
a.extend m
b.extend m
puts a == b # true for 1.9, false for 1.8 :(
@justinko
justinko / gist:1703386
Created January 30, 2012 08:43
Ruby 1.9 Delegator bug
require 'delegate'
A = Struct.new(:a)
class B < DelegateClass(A)
attr_accessor :foo
end
b = B.new(A.new(5))
puts b.inspect
puts b.respond_to?(:foo)
class NullLogger < Logger
def initialize(*args); end
def write(*input); true; end
end
Rails.application.assets.logger = NullLogger.new
class TemporaryLoggerLevel
LEVEL = Logger::ERROR
require 'spec_helper'
describe TCPSocket do
describe '#write_nonblock' do
let(:payload) { 'JUNK IN THE TUBES' }
it "writes if the operation won't block" do
setup_tcp do |client, peer|
client.write_nonblock(payload).should eq payload.length
peer.read(payload.length).should eq payload
@justinko
justinko / gist:1894922
Created February 23, 2012 20:36
Help for rspec-users mailing list question
require 'spec_helper'
shared_examples_for 'employee signin' do
it { should have_selector('title', text: employee.emp_full_name) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
describe "Authentication" do
@justinko
justinko / ruby_code.rb
Created February 26, 2012 01:35
API Driven Design

Let's say I have a file that needs to be parsed and validated. Instead of jumping to tests, I want to first think about the objects involved and their messages.

I would then write out the above code.

After I'm satisfied with the objects and their interactions, I would then write a spec for FileObject or FileValidator#validate.

I consider this approach a technique above TDD. Before, when I used to jump straight into the specs, it would often result in working code, but bad design. TDD encourages you to think about design, but only in your head (which is obviously not as efficient as writing it out, not to mention the visual feedback).

NOTE: The very first thing I do is create a functional/high-level spec, so this approach only applies on the implementation level (units).

@justinko
justinko / gist:1916757
Created February 26, 2012 13:37
No instance variables in Rails controllers
class PostsController < ActiveRecord::Base
helper_method :posts, :post
def index; end
def create
if post.save
end
end
@justinko
justinko / seed.rb
Created February 27, 2012 01:24
FactoryGirl for dev seeding and tests
# lib/tasks/app.rake
namespace :app do
desc 'Seed the current env db with "dummy" data'
task seed: :environment do
require './lib/seed'
Seed.start
end
end
@justinko
justinko / application.rb
Created March 16, 2012 10:33
Setup Devise in your application.rb instead of the models
module MyApp
class Application < Rails::Application
config.to_prepare do
User.devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
Admin.devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
end
end