how to use padrino helper methods in rspec

守給你的承諾、 提交于 2019-12-04 16:52:28

This was answered in their issue tracker: https://github.com/padrino/padrino-framework/issues/930#issuecomment-8448579

Cut and paste from there:

Shorthand helpers (this is a Sinatra, not a Padrino feature, by the way) are hard to test for a reason:

MyApp.helpers do
  def foo

  end
end

is a shorthand for:

helpers = Module.new do
  def foo
  end
end

MyApp.helpers helpers

So the problem for testability is obvious: the helpers are an anonymous module and its hard to reference something that is anonymous. The solution is easy: make the module explicit:

module MyHelpers
  def foo
  end
end

MyApp.helpers MyHelpers

and then test it in any fashion you want, for example:

describe MyHelpers do
  subject do
    Class.new { include MyHelpers } 
  end

  it "should foo" do
    subject.new.foo.should == nil
  end
end

Also, your example doesn't work because it assumes that helpers are global, which they are not: they are scoped by application.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!