{"id":616,"date":"2022-08-30T15:04:19","date_gmt":"2022-08-30T15:04:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/cant-access-image_tag-from-helper-module-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:19","modified_gmt":"2022-08-30T15:04:19","slug":"cant-access-image_tag-from-helper-module-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cant-access-image_tag-from-helper-module-collection-of-common-programming-errors\/","title":{"rendered":"Can&#39;t access image_tag from helper module-Collection of common programming errors"},"content":{"rendered":"<p>I would like to test the following helper module function:<\/p>\n<pre><code>module UploadsHelper\n\n  def custom_img_tag(upload, width, height, id)\n     if width &gt; Upload::MAX_CROP_WIDTH\n       image_tag(upload.photo.url(:original), :id =&gt; \"box\", :width =&gt; Upload::MAX_CROP_WIDTH, :height =&gt; (height*Upload::MAX_CROP_WIDTH\/width).to_i)\n     else\n       image_tag(upload.photo.url(:original), :id =&gt; \"box\")\n     end\n   end\n\nend\n<\/code><\/pre>\n<p>However when I run the following test:<\/p>\n<pre><code>describe UploadsController do\n  include UploadsHelper\n    describe \"custom_img_tag(upload, width, height, id)\" do\n           before(:each) do\n             @upload = Factory(:upload)\n             geo = Paperclip::Geometry.from_file(@upload.photo.to_file(:original))\n             @width   = geo.width\n             @height  = geo.height\n           end\n\n       it \"should return the original image tag for an image that is not wider than  MAX_CROP_WIDTH\" do\n         #custom_img_tag(@upload,@width, @heigth, \"cropbox\" ).should == '<img loading=\"lazy\" decoding=\"async\" id=\"cropbox\" width=\"500\" height=\"375\" src=\"\/system\/photos\/10\/original\/avatar.jpg?1311044917\" alt=\"Avatar\" style=\"display: none;\" \/>'\n       end\n     end\n<\/code><\/pre>\n<p>I get the following error:<\/p>\n<pre><code>Failure\/Error: custom_img_tag(@upload,@width, @heigth, \"cropbox\" ).should == '<img loading=\"lazy\" decoding=\"async\" id=\"cropbox\" width=\"500\" height=\"375\" src=\"\/system\/photos\/10\/original\/avatar.jpg?1311044917\" alt=\"Avatar\" style=\"display: none;\" \/>'\n     NoMethodError:\n       You have a nil object when you didn't expect it!\n<\/code><\/pre>\n<p>Why do I get this error and how can I test this method?<\/p>\n<p>Update: I added the following to the spec test file:<\/p>\n<pre><code>include ActionView::Helpers \n<\/code><\/pre>\n<p>Which produces the following error:<\/p>\n<pre><code>NameError:\n       undefined local variable or method `config' for #<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:07:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I would like to test the following helper module function: module UploadsHelper def custom_img_tag(upload, width, height, id) if width &gt; Upload::MAX_CROP_WIDTH image_tag(upload.photo.url(:original), :id =&gt; &#8220;box&#8221;, :width =&gt; Upload::MAX_CROP_WIDTH, :height =&gt; (height*Upload::MAX_CROP_WIDTH\/width).to_i) else image_tag(upload.photo.url(:original), :id =&gt; &#8220;box&#8221;) end end end However when I run the following test: describe UploadsController do include UploadsHelper describe &#8220;custom_img_tag(upload, width, height, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}