ruby - How to test adding line to error log by causing rescue statement in rspec -
i have foo class.
class foo def open_url net::http.start("google.com") |http| # end rescue exception => e file.open("error.txt","a+"){|f| f.puts e.message } end end and want test rspec.
require_relative 'foo' describe foo describe "#open_url" "should put error log if connection fails" net::http.stub(:start).and_return(exception) # check if line error.txt added. end end end how can check if line inserted error.txt?
you read file writing
describe foo describe "#open_url" "should put error log if connection fails" net::http.stub(:start).and_raise foo.open_url file = file.open("error.txt", "r") # ... end end end an alternative check file opened: along lines of this
file.should_receive(:open).with("error.txt", "a+", {|f| f.puts e.message })
Comments
Post a Comment