Test Helper Method with Minitest

I would like to test the helper method using Minitest ( minitest-rails ), but the helper method depends on current_user , the Devise helper method is available for controllers and view .

application / helpers / application_helper.rb

 def user_is_admin? # want to test current_user && current_user.admin? end 

test / helpers / application_helper_test.rb

 require 'test_helper' class ApplicationHelperTest < ActionView::TestCase test 'user is admin method' do assert user_is_admin? # but current_user is undefined end end 

Note that I can check for other helper methods that are not dependent on current_user .

+6
source share
1 answer

When you test the helper in Rails, the helper is included in the test object. (The test object is an instance of ActionView :: TestCase.) Your helper method, user_is_admin? expects that there will also be a method called current_user . On controller and view_context objects, this method is provided by Devise, but it is not yet on your test object. Add it:

 require 'test_helper' class ApplicationHelperTest < ActionView::TestCase def current_user users :default end test 'user is admin method' do assert user_is_admin? end end 

The object returned by current_user is up to you. Here we returned the data. Here you can return any object that makes sense in the context of your test.

+12
source

All Articles