如何使用 Rspec 测试 Sinatra 中的辅助块?

发布于 2024-09-02 04:53:55 字数 155 浏览 3 评论 0 原文

我正在编写一个 sinatra 应用程序并使用 rspec 和rack/test 对其进行测试(如 sinatrarb.com 上所述)。
到目前为止,一切都很棒,直到我将一些相当程序化的代码从我的域对象移到 西纳特拉的帮手。

从那时起,我一直试图弄清楚如何单独测试这些?

I'm writing a sinatra app and testing it with rspec and rack/test (as described on sinatrarb.com).
It's been great so far, until I moved some rather procedural code from my domain objects to
sinatra helpers.

Since then, I've been trying to figure out how to test these in isolation ?

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

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

发布评论

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

评论(4

你丑哭了我 2024-09-09 04:53:55

我通过将帮助器方法放入其自己的模块中来单独测试我的 sinatra 帮助器。
由于我的 sinatra 应用程序比通常的 hello world 示例稍大一些,因此我需要将其分成更小的部分。通用助手的模块非常适合我的用例。

如果您编写一个快速演示,并在 helpers { ... } 块中定义辅助方法,我认为测试它是绝对必要的。无论如何,任何生产中的 sinatra 应用程序都可能需要更多的模块化。

# in helpers.rb
module Helpers
  def safe_json(string)
    string.to_s.gsub(/[&><']/) { |special| {'&' => '\u0026', '>' => '\u003E', '<' => '\u003C', "'" => '\u0027'}[special] }
  end
end

# in app.rb
helpers do
  include Helpers
end

# in spec/helpers_spec.rb
class TestHelper
  include Helpers
end

describe 'Sinatra helpers' do
  let(:helpers) { TestHelper.new }

  it "should escape json to inject it as a html attribute"
    helpers.safe_json("&><'").should eql('\u0026\u003E\u003C\u0027')
  end
end

I test my sinatra helpers in isolation by putting the helper methods within its own module.
Since my sinatra application is a little bit bigger than the usual hello world example, I need to split it up into smaller parts. A module for the common helpers suits my use case well.

If you write a quick demo, and you define your helper methods within the helpers { ... } block, I don't think testing it is absolutely necessary. Any sinatra app in production, may require more modularity anyways.

# in helpers.rb
module Helpers
  def safe_json(string)
    string.to_s.gsub(/[&><']/) { |special| {'&' => '\u0026', '>' => '\u003E', '<' => '\u003C', "'" => '\u0027'}[special] }
  end
end

# in app.rb
helpers do
  include Helpers
end

# in spec/helpers_spec.rb
class TestHelper
  include Helpers
end

describe 'Sinatra helpers' do
  let(:helpers) { TestHelper.new }

  it "should escape json to inject it as a html attribute"
    helpers.safe_json("&><'").should eql('\u0026\u003E\u003C\u0027')
  end
end
微凉 2024-09-09 04:53:55

实际上你不需要这样做:

helpers do
  include FooBar
end

因为你只需调用

helpers FooBar

helpers 方法需要一个要混合的模块列表和一个在以下位置进行类评估的可选块: https://github.com/sinatra/sinatra/blob/ 75d74a413a36ca2b29beb3723826f48b8f227ea4/lib/sinatra/base.rb#L920-L923

Actually you don't need to do:

helpers do
  include FooBar
end

Since you can just call

helpers FooBar

The helpers method takes a list of modules to mix-in and an optional block which is class-eval'd in: https://github.com/sinatra/sinatra/blob/75d74a413a36ca2b29beb3723826f48b8f227ea4/lib/sinatra/base.rb#L920-L923

硬不硬你别怂 2024-09-09 04:53:55

我也尝试过这个(需要清理一下才能重用)来将每个助手隔离在自己的环境中进行测试:

class SinatraSim
  def initialize
    ...set up object here...
  end
end

def helpers(&block)
  SinatraSim.class_eval(&block)
end

require 'my/helper/definition' # defines my_helper

describe SinatraSim do
  subject { SinatraSim.new(setup) }

  it "should do something"
    subject.expects(:erb).with(:a_template_to_render) # mocha mocking
    subject.my_helper(something).should == "something else"
  end
end

I've also tried this (which needs to be cleaned up a bit to be reusable) to isolate each helper in its own environment to be tested:

class SinatraSim
  def initialize
    ...set up object here...
  end
end

def helpers(&block)
  SinatraSim.class_eval(&block)
end

require 'my/helper/definition' # defines my_helper

describe SinatraSim do
  subject { SinatraSim.new(setup) }

  it "should do something"
    subject.expects(:erb).with(:a_template_to_render) # mocha mocking
    subject.my_helper(something).should == "something else"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文