我有一个图书模型,它是一个 ruby 脚本,可将价格分配给程序中提到的某些预定义图书标题。我正在使用 Ruby 1.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
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"
如果用户输入任何字母数字数据或负数或 0 或任何具有小数的数据,则根据程序要求用户仅输入每本书的价格的正整数。我尝试使用 RSpec 来模拟这种行为。
我目前的挑战是如何捕捉错误的用户输入、随之而来的消息并适当地处理它,以便规范通过。我已经尝试了几件事,但下面的规范目前遇到了一个无限循环,输入价格的格式错误。PFB 我的规格。
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
谁能指出我如何正确处理这个问题的正确方向。我能够模拟被要求重新输入错误输入数据的用户。我的问题是如何在作为规范的一部分正确输入时停止它(根据实际的程序逻辑)。
我确实尝试了来自类似问题的建议之一,但它似乎对我不起作用。我可能会遗漏一些东西。该代码也可以从 Github 克隆。谢谢你。