Permalink
Browse files

switch specs away from activerecord

  • Loading branch information...
1 parent d2d7528 commit 019eab60adbfb8407eb434a7798bb8d0b98c7954 @garybernhardt committed Apr 17, 2011
Showing with 35 additions and 11 deletions.
  1. +15 −5 lib/do_not_want.rb
  2. +18 −4 spec/do_not_want_spec.rb
  3. +2 −2 spec/gems/fake_gem.rb
View
@@ -1,10 +1,20 @@
class Object
def self.do_not_want!(method_name, reason)
+ begin
+ original_method = instance_method(method_name)
+ rescue NameError
+ original_method = nil
+ end
+
@@do_not_want_original_methods ||= {}
- @@do_not_want_original_methods[method_name] = instance_method(method_name)
- self.send :define_method, method_name do
- if DoNotWant.should_ignore_caller(caller)
- return @@do_not_want_original_methods[method_name].bind(self).call
+ @@do_not_want_original_methods[method_name] = original_method
+
+ self.send :define_method, method_name do |*args|
+ original_method = @@do_not_want_original_methods[method_name]
+ use_real_method = (original_method &&
+ DoNotWant.should_validate_for_caller(caller))
+ if use_real_method
+ return original_method.bind(self).call(*args)
end
raise DoNotWant::NotSafe.new(self.class, method_name, reason)
end
@@ -31,7 +41,7 @@ def initialize(called_class, called_method, reason)
end
end
- def self.should_ignore_caller(caller)
+ def self.should_validate_for_caller(caller)
/\/gems\//.match(caller[0])
end
end
View
@@ -8,23 +8,37 @@ def save; end
require 'do_not_want'
require 'gems/fake_gem'
+class Walrus
+ def be_killed_by!(killer)
+ die!
+ "killed by #{killer}"
+ end
+
+ def die!
+ end
+
+ do_not_want! :be_killed_by!, 'because dying sucks'
+end
+
describe 'do not want' do
- let(:model_object) { ActiveRecord::Base.new }
+ let(:walrus) { Walrus.new }
it "raises an error for unwanted method calls" do
+ walrus.should_not_receive(:die!)
expect do
- model_object.update_attribute(:attribute, :value)
+ walrus.be_killed_by!
end.to raise_error(DoNotWant::NotSafe)
end
it "lets other methods through" do
- model_object.class.should == ActiveRecord::Base
+ walrus.class.should == Walrus
end
context "caller filtering" do
it "ignores calls from gems" do
+ walrus.should_receive(:die!)
expect do
- call_activerecord_update_attribute
+ kill_walrus(walrus)
end.not_to raise_error
end
end
View
@@ -1,4 +1,4 @@
-def call_activerecord_update_attribute
- ActiveRecord::Base.new.update_attribute
+def kill_walrus(walrus)
+ walrus.be_killed_by!('kitty')
end

0 comments on commit 019eab6

Please sign in to comment.