如何将 Rails 助手导入到功能测试中

发布于 2024-10-02 08:51:45 字数 1055 浏览 2 评论 0原文

你好,我最近继承了一个项目,其中前开发人员不熟悉 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中相同的方式访问

我如何欺骗这个疯狂的设置来测试?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稚然 2024-10-09 08:51:45

如果您想测试助手,可以按照此处的示例操作:

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

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
撑一把青伞 2024-10-09 08:51:45

我发现的唯一解决方案是重构并使用一个像样的身份验证插件

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

萝莉病 2024-10-09 08:51:45

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

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

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'
送君千里 2024-10-09 08:51:45

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

include Devise::TestHelpers

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

sign_in users(:one)

而不是

current_user = users(:one)

所有功能测试都应该可以正常工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文