Permalink
Browse files

Merge pull request #9 from andyferra/attachment-disorder

Ensure that artifice is always deactivated when using a block
  • Loading branch information...
2 parents 00591c5 + 3db53b0 commit 4c0f63932bdff448d4306f175e1ccd203befac3b Andre Arko committed Aug 28, 2011
Showing with 32 additions and 3 deletions.
  1. +6 −3 lib/artifice.rb
  2. +26 −0 spec/artifice_spec.rb
View
@@ -20,8 +20,11 @@ def self.activate_with(endpoint)
replace_net_http(Artifice::Net::HTTP)
if block_given?
- yield
- deactivate
+ begin
+ yield
+ ensure
+ deactivate
+ end
end
end
@@ -128,4 +131,4 @@ def make_net_http_response(response)
end
end
end
-end
+end
View
@@ -152,4 +152,30 @@
it_should_behave_like "a working HTTP request"
end
end
+
+ describe "when activating with a block" do
+
+ before do
+ ::Net::HTTP.should == NET_HTTP
+ end
+
+ after do
+ ::Net::HTTP.should == NET_HTTP
+ end
+
+ it "deactivates automatically after the block is executed" do
+ Artifice.activate_with( lambda {} ) do
+ ::Net::HTTP.should == Artifice::Net::HTTP
+ end
+ end
+
+ it "deactivates even if an exception is raised from within the block" do
+ lambda {
+ Artifice.activate_with( lambda {} ) do
+ ::Net::HTTP.should == Artifice::Net::HTTP
+ raise 'Boom!'
+ end
+ }.should raise_error
+ end
+ end
end

0 comments on commit 4c0f639

Please sign in to comment.