How to import Rails helpers in to the functional tests

巧了我就是萌 提交于 2019-12-20 00:43:33

问题


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.

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
  include BannersHelper
  include UsersHelper
  include EventsHelper
end

Specifically session management. This is okay and working with the app but I am having problems writing tests for this.

A specific example. Some of the actions do a before_filter to see if a current_user is an admin. This current_user is normally set by a sessions_helper method that is shared in all our controllers So in order to properly test our controllers I need to be able to use this current_user method

I have tried this:

require 'test_helper'
require File.expand_path('../../../app/helpers/sessions_helper.rb', __FILE__)

class AppsControllerTest < ActionController::TestCase
  setup do
    @app = apps(:one)
    current_user = users(:one)
  end

  test "should create app" do
    assert_difference('App.count') do
      post :create, :app => @app.attributes
  end
end

The require statement finds the session_helper.rb okay but without the Rails magic it is not accessible in the same way with in the AppsControllerTest

How can I spoof this crazy setup to test?


回答1:


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




回答2:


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'



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/4160417/how-to-import-rails-helpers-in-to-the-functional-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!