{"id":232,"date":"2022-08-30T14:57:55","date_gmt":"2022-08-30T14:57:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/31\/testing-views-with-rspec-undefined-current_user-in-helper-file-when-rendering-view-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:57:55","modified_gmt":"2022-08-30T14:57:55","slug":"testing-views-with-rspec-undefined-current_user-in-helper-file-when-rendering-view-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/testing-views-with-rspec-undefined-current_user-in-helper-file-when-rendering-view-record-and-share-programming-errors\/","title":{"rendered":"Testing views with rspec &#8211; undefined current_user in helper file when rendering view-Record and share programming errors"},"content":{"rendered":"<p>I&#8217;m using rspec, with devise and trying to test my views. In a helper function which my view calls, I use the variable current_user This works fine in practice, but using rspec, I get a sad &#8216;undefinied variable or method current_user&#8217; error.<\/p>\n<p>I&#8217;ve tried a lot of things that don&#8217;t work.<\/p>\n<pre><code>@user = FactoryGirl.build_stubbed(:user)\nassign(current_user, @user)\nsign_in :user, @user\ndef current_user\n  @user\nend\ncurrent_user = @user\nview.stub!(:current_user).and_return @user\nassign[:currrnet_user] = @user\n<\/code><\/pre>\n<p>Anyone know how to do this? Thanks in advance.<\/p>\n<ol>\n<li>\n<p>So, the solution to helper methods is to stub them. I don&#8217;t really like this solution, because I see it as a bug, but it is the way to do what needs to be done.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-08-31 05:30:27. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using rspec, with devise and trying to test my views. In a helper function which my view calls, I use the variable current_user This works fine in practice, but using rspec, I get a sad &#8216;undefinied variable or method current_user&#8217; error. I&#8217;ve tried a lot of things that don&#8217;t work. @user = FactoryGirl.build_stubbed(:user) assign(current_user, [&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-232","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/232","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=232"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}