【发布时间】:2016-03-01 15:17:38
【问题描述】:
我正在从 XML 中提取一个值并使用该值来检查它是否存在于 PDF 文件中:
我拥有的 XML 是
<RealTimeLetter>
<Customer>
<RTLtr_Acct>0163426</RTLtr_Acct>
<RTLtr_CustomerName>LSIH JHTWVZ</RTLtr_CustomerName>
<RTLtr_CustomerAddress1>887 YPCLY THYZO SU</RTLtr_CustomerAddress1>
<RTLtr_CustomerAddress2 />
<RTLtr_CustomerCity>WOODSTOCK,</RTLtr_CustomerCity>
<RTLtr_CustomerState>GA</RTLtr_CustomerState>
<RTLtr_CustomerZip>30188</RTLtr_CustomerZip>
<RTLtr_ADAPreference>NONE</RTLtr_ADAPreference>
<RTLtr_Addressee>0</RTLtr_Addressee>
</Customer>
</RealTimeLetter>
PDF 文件包含客户名称和地址
LSIH JHTWVZ
887 YPCLY THYZO SU
WOODSTOCK, GA 30188
我正在使用 PDF Reader 和 Nokogiri gems 从 PDF 中读取文本,从 XML 中提取客户名称并检查 PDF 中是否包含客户名称。
PDF阅读器被解析为
require 'pdf_reader'
def parse_pdf
PDF::Reader.new(@filename)
end
@reader = file('C:\Users\ecz560\Desktop\30004_Standard.pdf').parse_pdf
require 'nokogiri'
@xml = Nokogiri::XML(File.open('C:\Users\ecz560\Desktop\30004Standard.xml'))
@CustName = @xml.xpath("//Customer[RTLtr_Loancust='0163426']//RTLtr_CustomerName").map(&:text).to_s
page_index = 0
@reader.pages.each do |page|
page_index = page_index+1
if expect(page.text).to include @CustName
valid_text = "Given text is present in -- #{page_index}"
puts valid_text
end
end
但我收到一个错误:
RSpec::Expectations::ExpectationNotMetError: expected "LSIH JHTWVZ\n 887 YPCLY THYZO SU\n WOODSTOCK, GA 30188\n Page 1 of 1" to include "[\"LSIH JHTWVZ\"]"
Diff:
@@ -1,2 +1,80 @@
-["LSIH JHTWVZ"]
+ LSIH JHTWVZ
+ 887 YPCLY THYZO SU
+ WOODSTOCK, GA 30188
./features/step_definitions/Letters/Test1_Letters.rb:372:in `block (2 levels) in <top (required)>'
./features/step_definitions/Letters/Test1_Letters.rb:370:in `each'
./features/step_definitions/Letters/Test1_Letters.rb:370:in `/^I validate the PDF content$/'
C:\Users\ecz560\Documents\GitHub\ATDD Local\features\FeatureFiles\Letters\Test1_Letters.feature:72:in `Then I validate the PDF content'
在理解问题在于我比较@Custname 的方式上。 我该如何解决这个问题?
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读“minimal reproducible example”。我们没有足够的信息来帮助您,我们无法复制问题。您没有向我们展示完整的错误信息。作为旁注,您对实例变量的使用表明您不了解如何使用它们。此外,在 Ruby 中,我们不会将
@CustName之类的名称用于变量或常量。 -
我用所有必需的信息编辑了问题。
标签: ruby rspec pdf-reader