Suppose we have a custom FormBuilder in Padrino, like the following:
class CustomFormBuilder < Padrino::Helpers::FormBuilder::AbstractFormBuilder
def foo(arg1, arg2, ...)
# do something with #template
end
end
What's the right way to test this?
It seems like the correct thing to do would be something like:
describe CustomFormBuilder do
it "renders the right output"
# ...
result = CustomFormBuilder.new(...).template.render
expect(result).to include 'expected-content'
end
end
It's not clear to me how to pull that off:
What's the right way to test this?
I figured this out after some effort. The idea is to make an object which represents the template, pass that to the FormBuilder, make another object for the model, and then see if the builder generates the correct 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