【问题标题】:Test Ruby module测试 Ruby 模块
【发布时间】:2017-01-05 15:01:16
【问题描述】:

我对 Ruby 和 RSpec 非常陌生。我想为这个模块创建基本的规范文件:

module DownloadAttemptsHelper

  def download_attempt_filter_select_options
    search_types = [
      [I18n.t("helpers.download_attempts.search_types_default"),          'default'          ],
      [I18n.t("helpers.download_attempts.search_types_account_id"),      'account_id'      ],
      [I18n.t("helpers.download_attempts.search_types_remote_ip"),        'remote_ip'        ],
    ]
    options_for_select(search_types)
  end
end

小测试

describe DownloadAttemptsHelper do
  it "does something" do
    expect(1).to be_odd
  end
end

我明白了:

`<top (required)>': uninitialized constant DownloadAttemptsHelper (NameError)

我需要导入目录路径和模块名称吗? 你能给我一些非常基本的例子来测试这个模块吗?

【问题讨论】:

  • 网上有很多教程和指南,怎么写specs,你试过吗?
  • 是的,但是我对这种语言太陌生了。请给我一些基本的例子好吗?
  • 它不是一种语言,它是一个测试框架。这是来自谷歌的第一个链接。 codeschool.com/courses/testing-with-rspec

标签: ruby-on-rails ruby rspec


【解决方案1】:

如果您使用的是 Rails,则需要确保在服务器启动时加载您的模块。打开rails c 并检查您是否可以在那里访问您的模块。如果没有,您可以执行此处描述的操作Auto-loading lib files in Rails 4

也就是说,让 Rails 自动加载 lib 目录中的类,如果那是你的助手所在的位置。

【讨论】:

    【解决方案2】:

    您必须在 rspec 测试文件中包含该模块,以使其方法在测试用例中可用:

    describe "DownloadAttemptsHelper" do
      include DownloadAttemptsHelper
    
      it "does something" do
        expect(1).to be_odd
      end
    end
    

    【讨论】:

    • 我再次遇到同样的错误:``': uninitialized constant DownloadAttemptsHelper (NameError)`。可能我需要指定目录和文件位置?
    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2023-03-29
    • 2012-07-27
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多