簡體   English   中英

"NoMethodError: undefined method `env' for nil:NilClass" 用於 Rails 中的 Helper 測試

[英]"NoMethodError: undefined method `env' for nil:NilClass" for Helper testing in rails

我試圖用谷歌搜索,但我在使用 Minitest(沒有 Rspec)時找不到關於 ActionView 的這個錯誤的任何地方

custom_user_groups_helper_test.rb

class CustomUserGroupsHelperTest < ActionView::TestCase
    test "should work custom user" do
      response_params  = gotta_get
   end
end

custom_user_groups_helper.rb

 module CustomUserGroupsHelper

  def self.gotta_get
    return true
  end

test_helper.rb與控制器測試配合良好,但是當我將其用於輔助測試時,它會引發此錯誤

Error:
CustomUserGroupsHelperTest#test_should_work_custom_user:
NoMethodError: undefined method `env' for nil:NilClass

我也嘗試了另一種解決方案

使用 Rspec 測試設計時,“setup_controller_for_warden”錯誤中的“未定義方法‘env’ for nil:NilClass”

但這似乎是針對控制器的,沒有一個解決方案是針對輔助測試的。 來自 Devise 的ActionView::TestCase include的內容

我試圖包括這些

include Devise::TestHelpers
include Devise::Test::IntegrationHelpers
include Devise::Test::ControllerHelpers

正如 StackOverflow 上的解決方案之一所說

Devise::TestHelpers 應該只包含在控制器測試中,因此您需要通過將其添加到您的測試助手中來將其移動到 ActionController::TestCase 中:

  1. Then what to include for Helper test which inherits ActionView::TestCase
  2. How to test helpers in the Minitest framework.

但我想include Devise::Test::IntegrationHelpersinclude Devise::Test::ControllerHelpers是用於不同 rails 版本的控制器。

錯誤

在此處輸入圖片說明

更新了錯誤堆棧跟蹤

在此處輸入圖片說明

測試運行的堆棧跟蹤

    CustomUserGroupsHelperTest: test_should_work_custom_user
    --------------------------------------------------------
    (0.1ms)  ROLLBACK
  E    (0.1ms)  BEGIN

您似乎從根本上誤解了 Rails 中的助手是什么以及應該如何測試它們。 這真的可以理解,因為它是一個非常模糊的術語,在不同的上下文中意味着不同的事物。

Rails 應用程序中的助手(如/app/helpers )只是混合到視圖上下文中的模塊,並為諸如生成 html 或在 Devise 的情況下從會話中獲取用戶等內容提供簡單的助手方法。

像這樣的測試:

require 'test_helper'
class CustomUserGroupsHelperTest < ActionView::TestCase
  test "should work custom user" do
    post :save_custom_groups
    assert_equal "save_custom_groups", @controller.action_name
  end
end

只是在所有可能的方式上都是錯誤的。 幫助程序不響應 HTTP 方法。 這更像是有史以來最奇怪的控制器測試嘗試。

要測試助手,您應該像普通的舊方法一樣對其進行測試。 給它輸入並測試輸出:

module ListHelper
  def ul(array)
    content_tag :ul do
      array.each do |obj|
        concat content_tag :li, obj
      end
    end
  end
end

class ListHelperTest < ActionView::TestCase
  test "should return the user's full name" do
    assert_dom_equal %{<ul><li>foo</li><li>bar</li></ul>}, ul(["foo", "bar"])
  end
end

如果您正在測試依賴於來自 Devise 的方法的幫助程序,例如current_useruser_signed_in? 等你應該只是存根這些方法。 輔助測試不是全棧的,因此沒有真正的會話。

您可以通過在self上存根 ActionView::TestCase 中的其他輔助方法。 例如,如果您使用摩卡咖啡,您會這樣做:

class UsersHelperTest < ActionView::TestCase

  def setup
    @user = users(:david)
    self.stubs(:current_user).returns(@user)
  end

  test "should link to the current user" do
    assert_dom_equal %{<a href="/user/#{@user.id}">@user.name</a>}, link_to_current_user
  end
end

Devise::Test::IntegrationHelpersDevise::Test::ControllerHelpers用於集成和控制器測試,您實際上正在驅動大部分框架,並且與輔助測試完全無關。 這些是測試助手的示例,這是您混合到測試中的一種行為。

您當然可以通過集成測試間接測試助手,這些測試呈現使用助手的視圖,但由於助手相對簡單,因此直接測試它們可能是個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM