How do you test a Rails controller method exposed as a helper_method?

后端 未结 5 2611
眼角桃花
眼角桃花 2021-02-18 17:22

They don\'t seem to be accessible from ActionView::TestCase

5条回答
  •  不要未来只要你来
    2021-02-18 17:53

    I've struggled with this for a bit, because the accepted answer didn't actually test whether the method was exposed as a helper method.

    That said, we can use the #helpers method to get a proxy for testing.

    For example:

    class FooController < ApplicationController
      private
    
      def bar
        'bar'
      end
    
      helper_method :bar
    end
    

    Can be tested with:

    require 'test_helper'
    
    class FooControllerTest < ActionController::TestCase
      test 'bar is a helper method' do
        assert_equal 'bar', @controller.helpers.bar
      end
    end
    

提交回复
热议问题