require File.dirname(__FILE__) + '/spec_helper.rb' require 'fault_injection.rb' include FaultInjection $LOAD_PATH << File.dirname(__FILE__) describe "Argument Checking" do it "raises if a line number is less than 0" do Proc.new{ inject_fault({:file => 'target_00.rb',:line => -5}) }.should raise_error(ArgumentError) end it "raises if a err_class is not a subclass of Exception" do Proc.new { inject_fault({:file => 'target_00.rb',:line => 1}, 3) }.should raise_error(ArgumentError) end end describe "'File & line' fault-injection." do require 'target_00.rb' after(:each) do clear end it "works on Hash format for file & line specification" do Proc.new{ target_func_00 }.should_not raise_error inject_fault({:file => 'target_00.rb', :line => 9}) begin target_func_00 violate "target_func_00 must raise RuntimeError" rescue $!.instance_of?(RuntimeError).should be_true $!.backtrace[0].should match(/target_00\.rb/) $target_00['one'].should be_eql(1) $target_00['two'].should be_nil $target_00['three'].should be_nil end end it "works on 'file:line' format injection command." do Proc.new{ target_func_00 }.should_not raise_error inject_fault "target_00.rb:9", SecurityError, "This is a error" Proc.new{ target_func_00 }.should raise_error(SecurityError,"This is a error") end end describe "Method tracing fault-injection" do require 'target_02.rb' after(:each) do clear end it "should check arguments in Array format command." do Proc.new{ Bar.new.bar }.should_not raise_error Proc.new{ inject_fault([]) }.should raise_error(ArgumentError) Proc.new{ inject_fault([:Array]) }.should raise_error(ArgumentError) end it "should inject faults properly with Array format command" do Proc.new{ Bar.new.bar }.should_not raise_error # raise on method calling chane, "Bar#bar --> Foo#foo" inject_fault [:Bar,:bar,:Foo,:foo],RangeError Proc.new{ Bar.new.bar }.should raise_error(RangeError) end end describe "Specifying Exception class" do require 'target_01.rb' it "can inject fault with specified Exception class" do inject_fault({:file => 'target_01.rb', :line => 3},ArgumentError) Proc.new{ target_func_01 }.should raise_error(ArgumentError) clear end end