How to import Rails helpers in to the functional tests

前端 未结 4 2012
谎友^
谎友^ 2021-01-19 16:48

Hi I recently inherited a project in which the former dev was not familiar with rails, and decided to put a lot of important logic into the view helpers.

cla         


        
相关标签:
4条回答
  • 2021-01-19 16:57

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

    0 讨论(0)
  • 2021-01-19 16:58

    If you want to test helpers, you can follow the example here:

    http://guides.rubyonrails.org/testing.html#testing-helpers

    class UserHelperTest < ActionView::TestCase
      include UserHelper       ########### <<<<<<<<<<<<<<<<<<<
    
      test "should return the user name" do
        # ...
      end
    end
    

    This is for unit tests on individual methods. I think that if you want to test at a higher level, and you will be using multiple controllers w/redirects, you should use an integration test:

    http://guides.rubyonrails.org/testing.html#integration-testing

    As an example:

    require 'test_helper'
     
    class UserFlowsTest < ActionDispatch::IntegrationTest
      fixtures :users
     
      test "login and browse site" do
        # login via https
        https!
        get "/login"
        assert_response :success
     
        post_via_redirect "/login", username: users(:david).username, password: users(:david).password
        assert_equal '/welcome', path
        assert_equal 'Welcome david!', flash[:notice]
     
        https!(false)
        get "/posts/all"
        assert_response :success
        assert assigns(:products)
      end
    end
    
    0 讨论(0)
  • 2021-01-19 17:03

    Why re-factor? You can very easily include helpers from your project in your tests. I did the following to do so.

    require_relative '../../app/helpers/import_helper'
    
    0 讨论(0)
  • 2021-01-19 17:23

    To be able to use Devise in your tests, you should add

    include Devise::TestHelpers
    

    to each ActionController::TestCase instance. Then in the setup method you do

    sign_in users(:one)
    

    instead of

    current_user = users(:one)
    

    All your functional tests should work fine, then.

    0 讨论(0)
提交回复
热议问题