gpt4 book ai didi

ruby-on-rails-3 - 无法从辅助模块访问 image_tag

转载 作者:行者123 更新时间:2023-12-04 03:10:57 25 4
gpt4 key购买 nike

我想测试以下辅助模块功能:

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!

为什么会出现此错误以及如何测试此方法?

更新:我在规范测试文件中添加了以下内容:

include ActionView::Helpers 

这会产生以下错误:

NameError:
undefined local variable or method `config' for #<RSpec

我怎样才能摆脱这个错误,原因是什么?

感谢您的帮助。

最佳答案

我在使用 Rails 3.1 RC 时也遇到了错误

NameError:
undefined local variable or method `config'

一些 Rails 源代码跟踪,我发现缺少的包括 ActionView::AssetPaths。

include ActionView::AssetPaths
include ActionView::Helpers::AssetTagHelper

关于ruby-on-rails-3 - 无法从辅助模块访问 image_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6742212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com