Skip to content

Instantly share code, notes, and snippets.

View wojtekmach's full-sized avatar

Wojtek Mach wojtekmach

View GitHub Profile
@wojtekmach
wojtekmach / active_link_to.rb
Created February 8, 2012 20:38
active_link_to + defaults
def it description = "", metadata = {}, &block
if metadata && !metadata.empty? # perhaphs just `if !metadata.empty?` will suffice?
# your code
else
old_it description, &block
end
end
@wojtekmach
wojtekmach / address.rb
Created April 3, 2012 22:49
ValidAttribute macros
require 'active_attr'
class Address
include ActiveAttr::Model
attribute :city
attribute :country
validates :city, presence: true
validates :city, inclusion: { in: ["Vatican"] }, if: lambda { |address| address.country == 'Vatican' }
@wojtekmach
wojtekmach / account_test.rb
Created May 3, 2012 09:45
Writing MiniTest extensions
require 'minitest/autorun'
module MiniTest::Assertions
def assert_changes(obj, method, exp_diff)
before = obj.send method
yield
after = obj.send method
diff = after - before
assert_equal exp_diff, diff, "Expected #{obj.class.name}##{method} to change by #{exp_diff}, changed by #{diff}"
@wojtekmach
wojtekmach / example.rb
Created July 8, 2012 09:37
ActiveModel::ValueObject
class Address
include ActiveModel::ValueObject
attribute :street
attribute :city
end
class User < ActiveRecord::Base
attr_accessible :address
serialize :address, Address
require 'minitest/autorun'
class SetTest < MiniTest::Unit::TestCase
def setup
@set = Set.new
end
def test_size
assert_equal 0, @set.size
@set.add 42
@wojtekmach
wojtekmach / set_spec.rb
Created July 18, 2012 13:16
Set + minitest/spec
require 'minitest/autorun'
class SetTest < MiniTest::Spec
before do
@set = Set.new
end
specify '#size' do
@set.size.must_equal 0
@set.add 42
describe UsersController do
describe '#show' do
before { get :show, id: user.id }
it { should respond_with :success }
it { should assign_to(:user).with(user) }
context 'with public user' do
let(:user) { Factory(:public_user) }
end
@wojtekmach
wojtekmach / test_all.rake
Created August 3, 2012 18:30
rake test:all
namespace :test do
Rake::TestTask.new(:all) do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
end
end
task default: 'test:all'
@wojtekmach
wojtekmach / context.rb
Created August 4, 2012 12:44
minitest/spec + context
class MiniTest::Spec
def self.context(desc, &block)
stack = MiniTest::Spec.describe_stack
name = [stack.last, desc].compact.join('::')
Class.new self do
@name = name
class_eval &block
end
end