12

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

Dorian
  • 2,571
  • 23
  • 33

4 Answers4

11

You can stub out the method on view.

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

This will also work in helper specs.

twe4ked
  • 2,832
  • 21
  • 24
4

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

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

David Chelimsky
  • 8,920
  • 2
  • 38
  • 30
  • 2
    Hello David, Thank you for answering so quickly. If I understand correctly your answer, views that use helpers defined in controllers with helper_method cannot be tested with rspec because rspec cannot access those helpers... Somehow that does not feel right... – Dorian May 19 '11 at 16:23
  • https://github.com/rspec/rspec-rails/issues/119#issuecomment-309978 seems to indicate that this should be fixed, however it appears not to be (as of rspec-rails 2.10.1) – Eric Drechsel Jun 04 '12 at 20:22
0

Check out this one: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method.

You can define helper inside the ApplicationController, and use it across the controllers AND views.

bobzsj87
  • 818
  • 8
  • 17