假设我们在Padrino中有一个自定义的FormBuilder,如下所示:
class CustomFormBuilder < Padrino::Helpers::FormBuilder::AbstractFormBuilder
def foo(arg1, arg2, ...)
# do something with #template
end
end
测试这个的正确方法是什么?
似乎正确的做法是:
describe CustomFormBuilder do
it "renders the right output"
# ...
result = CustomFormBuilder.new(...).template.render
expect(result).to include 'expected-content'
end
end
我不清楚如何解决这个问题:
测试这个的正确方法是什么?
答案 0 :(得分:0)
经过一番努力,我想出了这个。我们的想法是创建一个表示模板的对象,将其传递给FormBuilder,为模型创建另一个对象,然后查看构建器是否生成正确的HTML。
describe CustomFormBuilder do
let(:template) do
Class.new do
include Padrino::Helpers::OutputHelpers
include Padrino::Helpers::FormHelpers
include Padrino::Helpers::TagHelpers
end.new
end
it "makes a class" do
model = Class.new { include ActiveModel::Model }.new
builder = described_class.new template, model
expect(builder.helper_method :foo).to include 'class="expected-class"'
# or, if you're using rspec-html-matchers or something similar...
expect(builder.helper_method :foo).to \
have_tag('div', :with => { :class => 'bar' })
end
end