如何在 RSpec 上包含 Rails Helpers

发布于 2025-01-08 18:32:14 字数 746 浏览 2 评论 0原文

我试图包含一些帮助程序来使用 rspec 进行测试,但没有运气。

我做了什么:

在我的 spec 文件夹下创建了一个 support/helpers.rb 文件。

support/helpers.rb

module Helpers
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TextHelper
end

并尝试在 spec_helper.rb 中请求此文件。

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'rubygems'
require 'spork'
require 'support/helpers'

Spork.prefork do
.
.
end

这会生成以下错误:

/spec/support/helpers.rb:2:in `<module:Helpers>': uninitialized constant Helpers::ActionView (NameError)

我应该如何执行此帮助程序才能与 Rspec 一起使用?

谢谢。

I'm trying to include some helpers to test with rspec but no luck.

What I did:

created a support/helpers.rb file under my spec folder.

support/helpers.rb

module Helpers
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TextHelper
end

and tried to require this file in spec_helper.rb.

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'rubygems'
require 'spork'
require 'support/helpers'

Spork.prefork do
.
.
end

this generates the following error:

/spec/support/helpers.rb:2:in `<module:Helpers>': uninitialized constant Helpers::ActionView (NameError)

How should I do this helpers to be available with Rspec?

Thanks.

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

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

发布评论

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

评论(3

橘虞初梦 2025-01-15 18:32:14

一旦 Rails 堆栈可用,我通常会包含此代码以要求我的 spec/support 子目录下的所有内容:

Spork.prefork do

  # ...

  Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

  RSpec.configure do |config|
    config.include MyCustomHelper

    # ...
  end
end

请注意,这将在所有示例类型(控制器、模型、视图、帮助者等)。您可以通过传递 :type 参数来缩小范围:

config.include MyControllerHelper, :type => :controller

I normally include this code to require everything under my spec/support subdirectory once the Rails stack is available:

Spork.prefork do

  # ...

  Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

  RSpec.configure do |config|
    config.include MyCustomHelper

    # ...
  end
end

Note that this will include MyCustomHelper in all example types (controllers, models, views, helpers, etc.). You can narrow that down by passing a :type parameter:

config.include MyControllerHelper, :type => :controller
英雄似剑 2025-01-15 18:32:14

直接将您需要的模块包含在规范文件中:

include PostsHelper

Include the Module you need directly in the spec file:

include PostsHelper
绮烟 2025-01-15 18:32:14

(Rails 7) 您还可以在例如 application.rb 中扩展帮助器查找位置,例如

# https://guides.rubyonrails.org/configuring.html#configuring-action-view
config.helpers_paths << 'path/to/unusual/helper/location.rb'

(Rails 7) You can also extend the helpers lookup location in e.g. application.rb like

# https://guides.rubyonrails.org/configuring.html#configuring-action-view
config.helpers_paths << 'path/to/unusual/helper/location.rb'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文