Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Controller helper methods not available for Views Specs

I have a helper method called current_user in my Application Controller (used with Authlogic).

Spec for views using that helper fail (but the view is working when i use the browser)

ActionView::Template::Error: undefined local variable or method 'current_user' for #<#<Class:0x0000000229b060>:0x00000002004248>

I use rspec 2.6.0.

Anyone had the same problem? Please advice. Thanks

like image 755
Dorian Avatar asked May 19 '11 10:05

Dorian


People also ask

How do you access the helper method in controller?

Instead of including the helper module inside the controller you can use the ActionController::Base. helpers available inside your action to have access to every helper method you use inside your views!

What are view helpers in Rails?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.

Why helper is used in Rails?

Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better.


2 Answers

You can stub out the method on view.

view.stub(:current_user).and_return(user)

This will also work in helper specs.

like image 75
twe4ked Avatar answered Nov 06 '22 20:11

twe4ked


Controller-defined helper methods are not included in the helper object.

http://relishapp.com/rspec/rspec-rails/dir/helper-specs/helper-spec

like image 4
David Chelimsky Avatar answered Nov 06 '22 20:11

David Chelimsky