如何在 RSpec 上包含 Rails Helpers
我试图包含一些帮助程序来使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一旦 Rails 堆栈可用,我通常会包含此代码以要求我的
spec/support
子目录下的所有内容:请注意,这将在所有示例类型(控制器、模型、视图、帮助者等)。您可以通过传递
:type
参数来缩小范围:I normally include this code to require everything under my
spec/support
subdirectory once the Rails stack is available: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:直接将您需要的模块包含在规范文件中:
Include the Module you need directly in the spec file:
(Rails 7) 您还可以在例如
application.rb
中扩展帮助器查找位置,例如(Rails 7) You can also extend the helpers lookup location in e.g.
application.rb
like