Skip to content

Instantly share code, notes, and snippets.

View bkempner's full-sized avatar

Ben Kempner bkempner

  • Auto List
  • Austin, TX
View GitHub Profile
pry(main)> a = [1,2,3]
=> [1, 2, 3]
pry(main)> a[2..3]
=> [3]
pry(main)> a[3..4] # out of bounds?
=> []
pry(main)> a[3]
=> nil
pry(main)> a[4]
=> nil
foo = case
when true
'foo'
else
'bar'
end
# foo = 'foo'
foo = case
when true
require 'spec_helper'
describe 'wtf' do
it "doesn't allow calls to a mocked method with different args" do
mock(User).find
User.find(1)
User.find(23) # where uses find internally
end
# error unexpected find(23) called
require 'spec_helper'
describe 'wtf' do
it "doesn't allow non-mocked method calls if any methods are mocked" do
u = User.new
mock(u).first_name
u.first_name
u.last_name
end
class_eval traced_method, __FILE__, __LINE__
## spec
it '#index' do
mock(controller).index
get :index
response.should be_success
end
## controller
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 >
ruby-1.9.2-p180 :002 >
ruby-1.9.2-p180 :003 >
ruby-1.9.2-p180 :004 > Snippet::Cheap < Snippet
=> true
ruby-1.9.2-p180 :005 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> [Snippet::Cheap(id: integer, location_id: integer, listing_id: integer, type: string, content: text, approved: boolean)]
ruby-1.9.2-p180 :006 >
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 >
ruby-1.9.2-p180 :002 >
ruby-1.9.2-p180 :003 >
ruby-1.9.2-p180 :004 >
ruby-1.9.2-p180 :005 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> []
ruby-1.9.2-p180 :006 > Snippet::Cheap.new => #<Snippet::Cheap id: nil, location_id: nil, listing_id: nil, type: "Snippet::Cheap", content: nil, approved: false>
ruby-1.9.2-p180 :007 > ruby-1.9.2-p180 :008 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> [Snippet::Cheap(id: integer, location_id: integer, listing_id: integer, type: string, content: text, approved: boolean)]
@bkempner
bkempner / gist:1093545
Created July 19, 2011 19:53
Rspec wtf?
## Controller
class FooController < ActionController::Base
def wtf
SomeObject.foo
end
end
def test_class(o)
case o.class
when Foo
puts "foo class"
else
puts "not found"
end
end
o = Foo.new