无法从辅助模块访问 image_tag [英] Can't access image_tag from helper module
本文介绍了无法从辅助模块访问 image_tag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想测试以下辅助模块功能:
I would like to test the following helper module function:
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
但是当我运行以下测试时:
However when I run the following test:
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!
为什么会出现此错误以及如何测试此方法?
Why do I get this error and how can I test this method?
更新:我在规范测试文件中添加了以下内容:
Update: I added the following to the spec test file:
include ActionView::Helpers
产生以下错误:
NameError:
undefined local variable or method `config' for #<RSpec
我怎样才能摆脱这个错误,原因是什么?
How can I get rid of this error and what is the cause?
感谢您的帮助.
推荐答案
我也遇到了使用 Rails 3.1 RC 的错误
I also got hit with the error working with Rails 3.1 RC
NameError:
undefined local variable or method `config'
一些 Rails 源代码跟踪,我发现缺少 include ActionView::AssetPaths.
Some Rails source tracing and I discovered the missing include ActionView::AssetPaths.
include ActionView::AssetPaths
include ActionView::Helpers::AssetTagHelper
这篇关于无法从辅助模块访问 image_tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文