Skip to content

Instantly share code, notes, and snippets.

@ogredude
ogredude / .watchr
Created July 8, 2011 21:56
My .watchr with red/green and Growl support
# Run me with:
#
# $ watchr .watchr
# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def growl(result)
result_lines = result.split("\n")
context "stores" do
it "should accept an array of Items and give back an array of Stores" do
i = Factory(:item)
i2 = Factory(:item)
i3 = Factory(:item, :auction => i2.store)
Item.stores([i,i2,i3]).should == [i.store, i2.store]
end
end
# Block helper
def block_to_partial(partial_name, options = {}, &block)
options.merge!(:body => capture(&block))
concat(render(:partial => partial_name, :locals => options), block.binding)
end
def block(title, options = {}, &block)
block_to_partial('layouts/block', options.merge(:title => title), &block)
end
@ogredude
ogredude / gist:1078874
Created July 12, 2011 20:13 — forked from szymon-przybyl/gist:1078859
UsersConroller#create spec
describe 'POST create' do
context 'with valid params' do
before(:each) { User.any_instance.stub!(:valid?).and_return(true) }
it 'creates new user and redirects to the home page with a notice' do
post :create, :user => Factory.attributes_for(:user, :username => 'dude')
assigns[:user].should be_persisted
# assigns[:user].username.should eq('dude') # Unnecessary, model tests should cover this
flash[:notice].should_not be_blank # be_blank instead of be_nil
response.should redirect_to(root_path) # path here, not url
end
require 'spec_helper'
describe Item do
it {should belong_to(:store)}
[:stock_number, :manufacturer, :model].each do |attr|
it "must have a(n) #{attr}" do
i = Factory.build(:item, attr.to_s => nil)
rspec spec
....................../Library/Ruby/Gems/1.8/gems/rspec-rails-2.6.1/lib/rspec/rails/matchers/redirect_to.rb:5: warning: multiple values for a block parameter (2 for 1)
from /Library/Ruby/Gems/1.8/gems/rspec-expectations-2.6.0/lib/rspec/matchers/matcher.rb:23
...../Library/Ruby/Gems/1.8/gems/rspec-rails-2.6.1/lib/rspec/rails/matchers/redirect_to.rb:5: warning: multiple values for a block parameter (2 for 1)
from /Library/Ruby/Gems/1.8/gems/rspec-expectations-2.6.0/lib/rspec/matchers/matcher.rb:23
.........................../Library/Ruby/Gems/1.8/gems/rspec-rails-2.6.1/lib/rspec/rails/matchers/redirect_to.rb:5: warning: multiple values for a block parameter (2 for 1)
from /Library/Ruby/Gems/1.8/gems/rspec-expectations-2.6.0/lib/rspec/matchers/matcher.rb:23
...../Library/Ruby/Gems/1.8/gems/rspec-rails-2.6.1/lib/rspec/rails/matchers/redirect_to.rb:5: warning: multiple values for a block parameter (2 for 1)
from /Library/Ruby/Gems/1.8/gems/rspec-expectations-2.6.0/lib/rspec/matchers/matcher.rb:23
..
def update_store_cart
store = Store.find(params[:store_id])
params[:item_ids] ||= []
params[:item_ids].each do |i|
Cart.add(current_user.id, i)
end
current_user.items.each do |i|
Cart.drop(current_user.id, i.id) unless params[:item_ids].include?(i.id.to_s) || i.store != store
it "should accept nested attributes for Answer" do
question = Factory.attributes_for(:question)
question[:answers_attributes] = [Factory.attributes_for(:answer)]
survey = Factory.build(:survey)
survey.question_attributes = question
lambda {
survey.save
}.should change(Answer, :count).by(1)
end
Uncaught TypeError: Object http://localhost:3000/surveys/new# has no method 'prev'
@ogredude
ogredude / RSpec profile
Created July 22, 2011 18:53
Slow specs
User must have a(n) username
0.67032 seconds ./spec/models/user_spec.rb:12
User should accept nested attributes for Department
0.59989 seconds ./spec/models/user_spec.rb:43
User should accept nested attributes for Company
0.58664 seconds ./spec/models/user_spec.rb:19
User should accept a Company object as company
0.5732 seconds ./spec/models/user_spec.rb:27
User should not save a blank Company
0.56833 seconds ./spec/models/user_spec.rb:35