Exception 如何捕获模拟的无限循环行为,并使用RSpec使用救援块处理它
我有一个图书模型,它是一个ruby脚本,为程序中提到的某些预定义图书标题指定价格。我使用的是Ruby1.9.3-p327和rspec 2.11.0Exception 如何捕获模拟的无限循环行为,并使用RSpec使用救援块处理它,exception,rspec2,infinite-loop,ruby-1.9.3,rescue,Exception,Rspec2,Infinite Loop,Ruby 1.9.3,Rescue,我有一个图书模型,它是一个ruby脚本,为程序中提到的某些预定义图书标题指定价格。我使用的是Ruby1.9.3-p327和rspec 2.11.0 #class RspecLoopStop < Exception; end class Book attr_accessor :books def initialize books puts "Welcome to setting book price program" @books = books end
#class RspecLoopStop < Exception; end
class Book
attr_accessor :books
def initialize books
puts "Welcome to setting book price program"
@books = books
end
def get_prices
puts "Please enter appropriate price for each book item:-"
count = 0
@books = @books.inject({}) { |hash, book|
print "#{book.first}: "
price = STDIN.gets.chomp
while (price !~ /^[1-9]\d*$/ && price != "second hand")
puts "Price cannot be 0 or a negative integer or in decimal format or alphanumeric. \nPlease input appropriate duration in integer"
price = STDIN.gets.chomp #gets.chomp - throws error
end
price == "second hand" ? price = "100" : price #takes a default price
hash[book.first] = price.to_i
hash
}
end
end
books = {"The Last Samurai" => nil,
"Ruby Cookbook" => nil,
"Rails Recipes" => nil,
"Agile Development with Rails" => nil,
"Harry Potter and the Deathly Hallows" => nil}
book_details = Book.new(books)
book_details.get_prices
#puts "\n*******Books:#{book_details.books}******\n"
谁能给我指出正确的方向,告诉我如何正确地处理这个问题。我能够模拟用户被要求重新输入错误输入的数据。我的问题是,作为规范的一部分,如果输入正确,如何停止(根据实际的程序逻辑)
我确实尝试了来自的一个建议,但它似乎对我不起作用。我可能错过了什么。还可以从Github克隆。多谢各位
require 'spec_helper'
describe Book do
before :each do
books = {"The Last Samurai" => nil,
"Ruby Cookbook" => nil,
"Rails Recipes" => nil,
"Agile Development with Rails" => nil,
"Harry Potter and the Deathly Hallows" => nil}
@book = Book.new(books)
end
describe "#getprice" do
it "Should get the price in the correct format or else return appropriate error" do
puts "\n************************************************************************\n"
book_obj = @book
STDIN.stub(:gets) { "40" }
book_obj.get_prices.should_not be_nil
end
it "Incorrect input format should return error message asking user to re input" do
puts "\n************************************************************************\n"
book_obj = @book
#STDIN.stub(:gets) { "40abc" }
#book_obj.get_prices.should be_nil --> adding this line of code goes into an infinite loop with the error message below
#Price cannot be 0 or a negative integer or in decimal format or alphanumeric. \nPlease input appropriate duration in integer\n
#STDOUT.should_receive(:puts).and_return("Price cannot be 0 or a negative integer or in decimal format or alphanumeric. \nPlease input appropriate duration in integer\n")
#the below two tests fails with syntax error - don't seem that easy to figure out what's going wrong
#STDOUT.should_receive("Price cannot be 0 or a negative integer or in decimal format or alphanumeric. \nPlease input appropriate duration in integer\n")
#\n towards the end is as in the third line of input the user is asked to re enter input in correct format
#STDOUT.should == "Price cannot be 0 or a negative integer or in decimal format or alphanumeric. \nPlease input appropriate duration in integer\n"
#begin #didn't work for me
# STDIN.stub(:gets) { "40abc" }
# book_obj.get_prices.should_raise RspecLoopStop
#rescue RspecLoopStop
# #exit
#end
begin
STDIN.stub(:gets) { "40abc" } #incorrect input prompts user to re enter price in correct format
book_obj.get_prices #how to catch the infinite loop as an exception and exit out of it say using rescue block
rescue #I guess this won't be called until the exception is correctly caught
STDIN.stub(:gets) { "85" }
book_obj.get_prices.should_not be_nil
end
end
end
end