【问题标题】:How to import Rails helpers in to the functional tests如何将 Rails 助手导入功能测试
【发布时间】:2010-11-11 23:52:35
【问题描述】:

您好,我最近接手了一个项目,之前的开发者对 Rails 不熟悉,因此决定将很多重要的逻辑放入视图助手中。

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

特别是会话管理。这没关系,可以使用该应用程序,但我在为此编写测试时遇到问题。

一个具体的例子。一些操作执行before_filter 以查看current_user 是否是管理员。这个current_user 通常由我们所有控制器共享的sessions_helper 方法设置 所以为了正确测试我们的控制器,我需要能够使用这个current_user 方法

我试过这个:

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

require 语句发现 session_helper.rb 没问题,但没有 Rails 魔法,它无法以与 AppsControllerTest 相同的方式访问

我怎样才能欺骗这个疯狂的设置来测试?

【问题讨论】:

  • AppsControllerTest 类中做include SessionsHelper 没有帮助?如果你打算在这个应用程序上积极开发,我强烈建议重构他们编写的代码,这没有意义,你会节省时间。听起来你只是想保持它运行,但没有时间投资。
  • 所以看起来帮助器被包括在内,因为应用程序控制器调用了 user_helper 中的一个方法,然后在 session_helper 中触发了一个错误。所以看起来他们好像被包括在内但不能互相交谈。z

标签: ruby-on-rails unit-testing ruby-on-rails-3 functional-testing


【解决方案1】:

我找到的唯一解决方案是重构并使用一个不错的身份验证插件

【讨论】:

    【解决方案2】:

    为什么要重构?您可以非常轻松地将项目中的助手包含在测试中。为此,我执行了以下操作。

    require_relative '../../app/helpers/import_helper'
    

    【讨论】:

      【解决方案3】:

      如果你想测试助手,你可以按照这里的例子:

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

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

      这是针对单个方法的单元测试。我认为,如果您想在更高级别进行测试,并且您将使用多个带重定向的控制器,您应该使用集成测试:

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

      举个例子:

      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】:

        为了能够在您的测试中使用 Devise,您应该添加

        include Devise::TestHelpers
        

        到每个ActionController::TestCase 实例。然后在你做的setup方法中

        sign_in users(:one)
        

        而不是

        current_user = users(:one)
        

        那么,你所有的功能测试应该都能正常工作。

        【讨论】:

        • 原始问题说没有使用设计。
        • 我假设你在哪里使用 Devise,因为常见的 current_user 变量。顺便说一句,您从来没有说过您没有使用Devise。很抱歉造成误解。
        猜你喜欢
        • 2010-10-01
        • 1970-01-01
        • 2014-08-26
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        相关资源
        最近更新 更多