Forum: RSpec [rspec] [rails] Specing a form builder

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1b32ae81cae0813e933ca2f8be830c9f?d=identicon&s=25 Chris Flipse (Guest)
on 2009-02-05 17:25
(Received via mailing list)
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?
1b32ae81cae0813e933ca2f8be830c9f?d=identicon&s=25 Chris Flipse (Guest)
on 2009-02-05 18:13
(Received via mailing list)
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.
This topic is locked and can not be replied to.