gem 中的规范视图助手

发布于 2024-10-31 21:08:45 字数 578 浏览 3 评论 0原文

我正在编写一个 Rails 3 Railtie,它定义了一些视图助手。现在我想知道如何指定视图帮助器方法,因为我无法实例化模块。我已经搜索并阅读了很多,但我就是无法让它发挥作用。

view_helper_spec.rb


require 'spec_helper'
module MyRailtie
  describe ViewHelper, :type => :helper do
    describe "style_tag" do
      it "should return a style tag with inline css" do
        helper.style_tag("body{background:#ff0}").should == 
            body{background:#ff0}
          
        EOT
      end
    end
  end
end

它总是抱怨“helper”未定义。似乎 :type => :helper 没有任何作用。

gemspec 在开发/测试模式下需要 rspec 和 rspec-rails gem。

I'm writing a rails 3 railtie which defines some view helpers. Now I wonder how to spec the view helper methods because I cannot instantiate a module. I already searched and read a lot, but I just couldnt get it working.

view_helper_spec.rb


require 'spec_helper'
module MyRailtie
  describe ViewHelper, :type => :helper do
    describe "style_tag" do
      it "should return a style tag with inline css" do
        helper.style_tag("body{background:#ff0}").should == 
            body{background:#ff0}
          
        EOT
      end
    end
  end
end

It always complains that "helper" is not defined. It seems that :type => :helper doesn't to anything.

The gemspec requires the rspec and the rspec-rails gems in development/ test mode.

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

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

发布评论

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

评论(1

冷月断魂刀 2024-11-07 21:08:45

我想我找到了解决方案(一旦你仔细想想,它就很明显)。只需将其放在规范的顶部即可:

require 'action_view'
require 'active_support'

include ViewHelper
include ActionView::Helpers

I think I found the solution (it's painfully obvious once you think about it). Just stick this at the top of your spec:

require 'action_view'
require 'active_support'

include ViewHelper
include ActionView::Helpers
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文