【发布时间】:2014-06-23 12:38:30
【问题描述】:
lib/export.rb
require 'writeexcel'
require "#{Rails.root}/lib/writeexcel"
class Export
include ApplicationHelper
def initialize(values)
@values_ids = values
end
def export_existing_data
# Create a new Excel workbook
workbook = WriteExcel.new('export.xls')
# Add a worksheet
worksheet = workbook.add_worksheet
format = workbook.add_format # Add a format
format.set_bold
format.set_bg_color('lightblue')
format.set_align('top')
format.set_size(12)
format.set_font('Arial')
# export codes .......
end
end
Rspec 代码
export_spec.rb
require "export"
describe TransformImport do
let!(:transform_export_usd) do
TransformImport.new(value_usd.id).export_existing_data
end
context ".export_existing_data" do
it "should have valid header" do
# here downloaded_file and export_excel_rows are helper method to compare
# export excel data, which is fine
downloaded_file.row(2).should == export_excel_rows
end
end
end
问题
运行此规范时出现以下错误
Export.export_existing_data should have valid header
Failure/Error: e = TransformImport.new(value_usd.id).export_existing_data
NameError:
uninitialized constant TransformImport::WriteExcel
# ./lib/transform_export.rb:13:in `export_existing_data'
# ./spec/lib/transform_export_spec.rb:120:in `block (2 levels) in <top (required)>'
Finished in 18.12 seconds
1 example, 1 failure
我在这里缺少什么。我也正确地包含了 require 块。此代码在我的应用程序端运行良好(即,当我从应用程序单击导出按钮时,它正在导出正确的数据)。
我是 rspec 的新手。请建议我,我做错了什么以及编写此规范的正确方法是什么?
谢谢
【问题讨论】:
-
尝试在规范文件中要求
writeexcel和#{Rails.root}/lib/writeexcel -
我也试过了,还是不行:(
-
实际上,我尝试在 spec 文件中要求
writeexcel(这不起作用),我也将它放在 spec_helper.rb 文件中仍然没有运气。那里出了点问题,我无法弄清楚..
标签: ruby rspec ruby-on-rails-3.2 tdd rspec-rails