Ruby還是個新手 - 我看了一些看似類似問題的答案,但說實話,我無法擺脫困境。使用救援並確保在代碼中間
我有一些讀取.csv文件的代碼。數據按每個用戶記錄分成40-50行組,並根據通過網站訪問的數據庫驗證行中的數據。
每個記錄都需要登錄,但是一旦該用戶登錄了.csv文件中的每一行,就可以檢查該用戶,直到到達下一個用戶,此時用戶註銷。
但是,如果發生錯誤(例如,網站上的結果不同於.csv文件上的預期結果),程序將停止運行。
我需要的東西,將 一)在告訴我發生了哪條線路上的文件錯誤 二)登錄該行輸出時,它的運行完畢後,和 三)重新啓動該程序從下一行的.csv文件後,
代碼我迄今低於事先
感謝,
彼得
require 'csv-mapper'
loginrequired = true
Given(/^I compare the User Details from rows "(.*?)" to "(.*?)"$/) do |firstrow, lastrow|
data = CsvMapper.import('C:/auto_test_data/User Data csv.csv') do
[dln, nino, pcode, endor_cd, ct_cd]
end
#Row number changed because Excel starts at 'row 1' and Ruby starts counting at 'row 0'
(firstrow.to_i-1..lastrow.to_i-1).each do |row|
@licnum1 = data.at(row).dln
@licnum2 = data.at(row+1).dln
@nino = data.at(row).nino
@postcode = data.at(row).pcode
@endor_cd = data.at(row).endor_cd
@ct_cd = data.at(row).ct_cd
#Login only required once for each new user-account
if
loginrequired == true
logon_to_vdr #def for this is in hooks
click_on 'P and D'
loginrequired = false
end
#This is the check against the database and is required for every line in the .csv file
check_ctcd #def for this is in hooks
#Need something in here to log errors and move on to the next line in the .csv file
#Compare the ID for the next record and logout if they're different
if @licnum1 == @licnum2
loginrequired = false
else
loginrequired = true`enter code here`
click_on 'Logout'
end
end
end
您是否能夠獲得回溯或錯誤消息?當發生無法預料的異常時,Ruby會自動將這些內容打印到STDERR。如果您從命令行運行腳本,則通常會在終端窗口中顯示此輸出。也許這是作爲一個計劃任務運行,所以你不能看到任何信息,因爲一旦錯誤發生,屏幕就會消失? – DRSE
我試圖類似,我發現我不能繞過RSpec的期望。它引發了這個異常,但沒有循環到下一個迭代 – Tom