ruby-on-rails - 如何测试作为 helper_method 公开的 Rails Controller 方法?

标签 ruby-on-rails methods testing helper

它们似乎无法从 ActionView::TestCase 访问

最佳答案

没错,辅助方法不会在 View 测试中公开 - 但它们可以在您的功能测试中进行测试。由于它们是在 Controller 中定义的,因此这是测试它们的正确位置。您的辅助方法可能定义为 private,因此您必须使用 Ruby 元编程来调用该方法。

app/controllers/posts_controller.rb:

class PostsController < ApplicationController

  private

  def format_something
    "abc"
  end
  helper_method :format_something
end

测试/功能/posts_controller_test.rb:

require 'test_helper'

class PostsControllerTest < ActionController::TestCase
  test "the format_something helper returns 'abc'" do
    assert_equal 'abc', @controller.send(:format_something)
  end
end

关于ruby-on-rails - 如何测试作为 helper_method 公开的 Rails Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2448063/

相关文章:

java 实例方法和变量的继承解析

qt - 使用 qt 模型测试

java - 测试自动化 : how to avoid conflicts on limited test resources?

ruby-on-rails - RSpec 夹具未加载

ruby-on-rails - 尝试为 Rails 中的用户名验证编写 REGEX

ruby-on-rails - 如何防止 find_by 将 nil 添加到数组

ruby-on-rails - 渲染状态 : 403 causes "Missing template" error

java - 在两个不同的类中调用方法 : Calling eachothers' methods

java - 在Java中调用获取时间的方法

testing - DBT 暴露