jnstq (owner)

Revisions

gist: 118102 Download_button fork
public
Public Clone URL: git://gist.github.com/118102.git
Embed All Files: show embed
snippet.txt #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'rubygems'
require 'activerecord'
require 'activesupport'
require "spec/autorun"
require "spec/mocks"
 
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
ActiveRecord::Migration.verbose = false
 
ActiveRecord::Migration.suppress_messages do
  ActiveRecord::Schema.define(:version => 0) do
    create_table :users, :force => true do |t|
      t.string :email, :password
    end
  end
end
 
class MethodRecorder < BlankSlate
  attr_reader :recorded_method_calls
  def initialize
    @recorded_method_calls = []
  end
 
  def method_missing(name, *args)
    recorder = MethodRecorder.new
    @recorded_method_calls << [name, args, recorder]
    recorder
  end
end
 
class MethodRecorderPlayback
 
  def initialize(target, recorded_method_calls)
    puts "#{target}"
    @target, @recorded_method_calls = target, recorded_method_calls
  end
 
  def playback
    call_recoreded_methods(@target, @recorded_method_calls)
  end
 
  private
 
  def call_recoreded_methods(target, recorded_method_calls)
    recorded_method_calls.each do |name, args, chained|
      call_recoreded_methods(target.send(name, *args), chained.recorded_method_calls)
    end
  end
end
 
module AnyInstance
  METHOD_RECODING = {}
  def new(*args, &blk)
    super.tap do |instance|
      MethodRecorderPlayback.new(instance, any_instance.recorded_method_calls).playback
    end
  end
 
  def any_instance
    METHOD_RECODING[self.class] ||= MethodRecorder.new
  end
end
 
class User < ActiveRecord::Base
  extend AnyInstance
end
 
# puts User.new.inspect
# User.any_instance.should_receive(:find).with(1)
# User.any_instance.should_receive(:destroy)
# puts User.any_instance.recorded_method_calls.inspect
#
# u = User.new
#
# p = MethodRecorderPlayback.new(u, User.any_instance.recorded_method_calls)
# p.playback
 
require 'spec/adapters/mock_frameworks/rspec'
 
module Spec
  module Adapters
    module MockFramework
      def teardown_mocks_for_rspec_with_any_instance
        teardown_mocks_for_rspec_without_any_instance
        AnyInstance::METHOD_RECODING.clear
      end
      alias_method_chain :teardown_mocks_for_rspec, :any_instance
    end
  end
end
 
describe User do
 
  before do
    User.any_instance.stub!(:username).and_return('Bob')
    User.any_instance.stub!(:password).and_return('hemligt')
  end
 
  it "should have a user name" do
    User.new.username.should eql('Bob')
  end
 
  it "should have a user name" do
    User.any_instance.should_receive(:password).and_return('hemligt')
    User.new.password.should eql('hemligt')
  end
 
end