Can't access image_tag from helper module
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
@开发者_StackOverflow中文版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
I get the following error:
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
Which produces the following error:
NameError:
undefined local variable or method `config' for #<RSpec
How can I get rid of this error and what is the cause?
Thanks for any assistance.
I also got hit with the error working with Rails 3.1 RC
NameError:
undefined local variable or method `config'
Some Rails source tracing and I discovered the missing include ActionView::AssetPaths.
include ActionView::AssetPaths
include ActionView::Helpers::AssetTagHelper
Well, I don't know why this would be, but my guess is that for some reason ActionView::Helpers must not be loaded in this spec. Try including ActionView::Helpers and see if that fixes it... The issue (from what you're reporting) is that when your custom_img_tag
method gets called it isn't able to call image_tag
for some reason.
精彩评论