[rspec] [rails] Specing a form builder


#1

I’m trying to set up and run a new form builder under rails, which lives
under the helpers directory, but isn’t really a helper. (Not a module,
so
doesn’t mix in well)

Short of dropping down into a view spec, how do I set up to actually
spec
the form builder?

describe RandomFormBuilder do
attr_reader :builder
before do
@object = mock_model(Foo)
@builder = RandomFormBuilder.new(:foo, @object, #template#, {}, nil)
end

describe “#text_field” do
it “should have a label” do
@builder.text_field(:foo).should have_tag(“label”, “Foo”)
end
end
end

class RandomFormBuilder < ActionView::Helpers::FormBuilder
def text_field(method, *args)
label(method)
super(method, *args)
end
end

That’s sort of what I’ve got at the moment. #template# supposed to be
an
object with access to all the normal helpers, I think.
using helper in that location gives me a NoMethodError.

using self in that location ends up with the helper trying to delegate
methods to Spec::Rails::Example::RailsExampleGroup::Subclass …

has anybody figured out what hoops this needs?


#2

got it. The normal FormBuilder class uses template as a pass through
for
the basic form fields.

before do
helper = Object.new.extend ActionView::Helpers::FormHelper
@object = mock_model(Foo)
@builder = RandomFormBuilder(:foo, @object, helper, {}, nil)
end

then all works beautifully.