我想测试以下辅助模块函数:
module UploadsHelper
def custom_img_tag(upload, width, height, id)
if width > Upload::MAX_CROP_WIDTH
image_tag(upload.photo.url(:original), :id => "box", :width => Upload::MAX_CROP_WIDTH, :height => (height*Upload::MAX_CROP_WIDTH/width).to_i)
else
image_tag(upload.photo.url(:original), :id => "box")
end
end
end
但是当我运行以下测试时:
describe UploadsController do
include UploadsHelper
describe "custom_img_tag(upload, width, height, id)" do
before(:each) do
@upload = Factory(:upload)
geo = Paperclip::Geometry.from_file(@upload.photo.to_file(:original))
@width = geo.width
@height = geo.height
end
it "should return the original image tag for an image that is not wider than MAX_CROP_WIDTH" do
#custom_img_tag(@upload,@width, @heigth, "cropbox" ).should == '<img id="cropbox" width="500" height="375" src="/system/photos/10/original/avatar.jpg?1311044917" alt="Avatar" style="display: none;">'
end
end
我收到以下错误:
Failure/Error: custom_img_tag(@upload,@width, @heigth, "cropbox" ).should == '<img id="cropbox" width="500" height="375" src="/system/photos/10/original/avatar.jpg?1311044917" alt="Avatar" style="display: none;">'
NoMethodError:
You have a nil object when you didn't expect it!
为什么我会收到此错误,如何测试此方法?
更新: 我将以下内容添加到spec测试文件中:
include ActionView::Helpers
产生以下错误:
NameError:
undefined local variable or method `config' for #<RSpec
我如何摆脱这个错误,原因是什么?
感谢您的帮助。
答案 0 :(得分:4)
我也遇到了使用Rails 3.1 RC
的错误NameError:
undefined local variable or method `config'
一些Rails源跟踪,我发现缺少包括ActionView :: AssetPaths。
include ActionView::AssetPaths
include ActionView::Helpers::AssetTagHelper
答案 1 :(得分:0)
嗯,我不知道为什么会这样,但我的猜测是由于某种原因,ActionView :: Helpers不能在此规范中加载。尝试包含ActionView :: Helpers并查看是否可以修复它...问题(来自您的报告)是,当您的custom_img_tag
方法被调用时,它无法调用image_tag
某种原因。