How to Test Helpers in Rails

How do I test helpers in Rails?

In rails 3 you can do this (and in fact it's what the generator creates):

require 'test_helper'

class YourHelperTest < ActionView::TestCase
test "should work" do
assert_equal "result", your_helper_method
end
end

And of course the rspec variant by Matt Darby works in rails 3 too

How to make helper methods available in a Rails integration test?

Your error messagae says:

NameError: undefined local variable or method 'my_preference'

which means you don't have access to my_preference method. To make that available in your class, you have to include the module in your class.

You have to include your module: SessionsHelper in your PreferencesTest class.

include SessionsHelper

Then, the instance method my_preference will be available for you to use in your test.

So, you want to do:

require 'test_helper'
require 'sessions_helper'

class PreferencesTest < ActionDispatch::IntegrationTest

include SessionsHelper

test "my test" do
...
get user_path(my_preference)
end

end

How do I test helpers in Rails 4?

You need to either enable the infer_spec_type_from_file_location! option, or explicitly set the test type, e.g.:


describe ApplicationHelper, type: :helper do
...
end

How to access a controller helper method from a helper test in Rails 4?

AFAIK (As far as I know), you cannot fix this without stubbing, or doing some change in your code, as essentially a helper file is just a module of itself that should be treated independent of where it's gonna be included. Who knows you might want to include such helper file inside your model files for example, in which incidentally the model file also has a method named can_access_participant_contact_data? but does differently from that one defined in the ApplicationController, therefore you cannot unit test this without specifying the context / base.

Possible Workarounds:

  • Stubbing:

    • Use Mocha or rework testing into RSpec
    • Or manually (maybe there's a better way) by:

      test "should return an asterisked string with spaces" do
      ParticipantsHelper.class_eval do
      define_method :can_access_participant_contact_data? do |arg|
      true
      end
      end

      redacted_contact_data(Participant.first, :name)
      end
  • Or, moving all your ApplicationController helper methods into a separate/existing helper file, say inside your already existing ApplicationHelper. Then afterwards, include that helper inside your other helper file that you are testing that is making use of the method/s. i.e.:

    # helpers/application_helper.rb
    module ApplicationHelper
    def can_access_participant_contact_data?(participant)
    # YOUR CODE
    end
    end

    # helpers/participants_helper.rb
    module ParticipantHelper
    include ApplicationHelper

    def redacted_contact_data participant, attribute_name
    attribute = participant.try(:contact_data).try(attribute_name)
    return attribute if can_access_participant_contact_data?(participant)
    return nil if attribute.blank?
    return attribute.gsub(/\S/i, '*') # Asterisked string
    end
    end
    • If using this approach, then two ways to call the helper method inside the controller:

      • Use Rails helpers method inside a controller:

        class ParticipantsController
        def show
        helpers.can_access_participant_contact_data?(@participant)
        end
        end
      • Or, include the helpers directly (I personally prefer the other approach just above)

        class ApplicationController < ActionController::Base
        include ApplicationHelper
        end

        class ParticipantsController < ApplicationController
        def show
        can_access_participant_contact_data?(@participant)
        end
        end
    • For the view files, you won't need to update any code.

How to import Rails helpers in to the functional tests

The only solution i found was to re-factor and use a decent auth plugin

How do I write a helper for an integration test in Rails?

Pick one:

module IntegrationHelperTest 
# ...
end

require 'test_helper'
require 'integration/integration_helper_test'
class PostIntegrationTest < ActionDispatch::IntegrationTest
include IntegrationHelperTest
# ...
end

or

class IntegrationHelperTest < ActionDispatch::IntegrationTest
# ...
end

require 'test_helper'
require 'integration/integration_helper_test'
class PostIntegrationTest < IntegrationHelperTest
# ..
end

Test helper method with Minitest

When you test a helper in Rails, the helper is included in the test object. (The test object is an instance of ActionView::TestCase.) Your helper's user_is_admin? method is expecting a method named current_user to also exist. On the controller and view_context objects this method is provided by Devise, but it is not on your test object, yet. Let's 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've returned a data fixture. You could return any object here that would make sense in the context of your test.

How to test a Rails 5 helper that relies on Devise signed_in? helper, with Minitest?

You need to stub all 3 methods and use an instance variable to control the value for two different states:

require 'test_helper'

class ApplicationHelperTest < ActionView::TestCase

def signed_in?
@signed_in
end

def new_user_session_path
'/users/new'
end

def destroy_user_session_path
'/users/new'
end

test 'returns Sign Out when signed in' do
@signed_in = true
assert_match 'Sign Out', sign_in_or_out_link
end

test 'returns Sign In when not signed in' do
@signed_in = false
assert_match /Sign In/, sign_in_or_out_link
end

end

Both tests pass:

~/codebases/tmp/rails-devise master*
❯ rake test
Run options: --seed 28418

# Running:

..

Finished in 0.007165s, 279.1347 runs/s, 558.2694 assertions/s.
2 runs, 4 assertions, 0 failures, 0 errors, 0 skips

Rails/RSpec - writing tests for original helper methods

Think about it this way:

The 'full_title' called in static_pages_spec.rb (including utilities.rb) is running the 'full_title' method described in application_helper.rb.

The application_helper_spec.rb is validating the string/value (page_title) passed through full_title.
If I'm not mistaken, it does this each time the full_title method is called in your tests.

Rails: Moving a helper method form a test to test_helper.rb

Turns out that I had to wrap the helper method into a module:

# test_helper.rb
class ActionController::TestCase
module CheatWithAuth do
def turn_off_authorization
# some code goes here
end
end

include CheatWithAuth
end

I still don't know why the original version didn't work.

The idea came from another answer:
How do I write a helper for an integration test in Rails?

Edit: Another solution just came from my friend:

# test_helper.rb
class ActiveSupport::TestCase
def turn_off_authorization
# some code goes here
end
end

Note that ActiveSupport::TestCase (not ActionController::TestCase) is being used here.



Related Topics



Leave a reply



Submit