繁体   English   中英

如何从Rails 4中的帮助程序测试访问控制器帮助程序方法?

[英]How to access a controller helper method from a helper test in Rails 4?

我在ApplicationController中定义了一个方法作为辅助方法。

helper_method :can_access_participant_contact_data?

我正在尝试为辅助文件中的辅助方法编写测试。 这个帮助器方法调用helper_method :can_access_participant_contact_data?

# In participants_helper.rb
#
def redacted_contact_data participant, attribute_name
  attribute = participant.try(:contact_data).try(attribute_name)
  return attribute if can_access_participant_contact_data?(participant)
  return nil       if attribute.blank?
  return attribute.gsub(/\S/i, '*') # Asterisked string
end

我在测试中所做的就是调用redacted_contact_data

require 'test_helper'

class ParticipantsHelperTest < ActionView::TestCase

   test "should return an asterisked string with spaces" do
     redacted_contact_data(Participant.first, :name)
   end

end

当我运行我的测试时,我收到了这条消息

undefined method `can_access_participant_contact_data?' for #<ParticipantsHelperTest:0x007fd6c7c6d608>

我一直在环顾四周,但我不知道如何解决这个问题。 我需要模拟can_access_participant_contact_data? 不知何故? 或者我可以将方法包括在测试中?

AFAIK(据我所知),你无法解决这个问题,如果没有存根,或者在你的代码中做一些改变,因为帮助文件基本上只是一个自身的模块,应该独立于它将被包含在哪里。 谁知道您可能希望在模型文件中包含此类帮助文件,例如,模型文件中还有一个名为can_access_participant_contact_data?的方法can_access_participant_contact_data? 但与ApplicationController定义的不同,因此如果不指定context / base,则无法对此进行单元测试。

可能的解决方法:

  • 存根:

    • 使用Mocha或返工测试到RSpec
    • 或者手动(也许有更好的方法):

       test "should return an asterisked string with spaces" do ParticipantsHelper.class_eval do define_method :can_access_participant_contact_data? do |arg| true end end redacted_contact_data(Participant.first, :name) end 
  • 或者,将所有ApplicationController帮助ApplicationController方法移动到单独的/现有的帮助程序文件中,例如在已有的ApplicationHelper 然后,将您正在测试的其他帮助文件中的帮助程序包含在使用方法/ sie中:

     # helpers/application_helper.rb module ApplicationHelper def can_access_participant_contact_data?(participant) # YOUR CODE end end # helpers/participants_helper.rb module ParticipantHelper include ApplicationHelper def redacted_contact_data participant, attribute_name attribute = participant.try(:contact_data).try(attribute_name) return attribute if can_access_participant_contact_data?(participant) return nil if attribute.blank? return attribute.gsub(/\\S/i, '*') # Asterisked string end end 
    • 如果使用这种方法,那么有两种方法可以调用控制器内的辅助方法:

      • 在控制器中使用Rails helpers方法:

         class ParticipantsController def show helpers.can_access_participant_contact_data?(@participant) end end 
      • 或者,直接包括帮助者(我个人更喜欢上面的其他方法)

         class ApplicationController < ActionController::Base include ApplicationHelper end class ParticipantsController < ApplicationController def show can_access_participant_contact_data?(@participant) end end 
    • 对于视图文件,您无需更新任何代码。

另一个想法是在“控制器测试”中进行如下“辅助测试”:

require 'test_helper'

class ParticipantsControllerTest < ActionController::TestCase
  setup do
    # do some initialization here. e.g. login, etc.
  end

  test "should return an asterisked string with spaces" do
    participant = ...
    get :show, id: participant.id
    assert_equal '...',  @controller.view_context.redacted_contact_data(...)
  end
end

其中,@ console是已由rails控制器测试框架定义的ParticipantsController对象(或者当控制器名称与* ControllerTest不同时,您可以显式定义它),view_context是辅助方法的对象(请参阅https://api.rubyonrails.org /classes/ActionView/Rendering.html#method-i-view_context了解更多详情)。

Helper方法通常引用控制器对象和/或方法(如会话,请求),因此有时很难仅在test / helpers / *中进行单元测试。 这就是我在这种情况下在控制器中测试助手的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM