views:

14

answers:

1

I have a rubygem that defines a custom SemanticFormBuilder class which adds a new Formtastic input type. The code works as expected, but I cannot figure out how to add tests for it. I was thinking I could do something like load up Formtastic, call semantic_form_for, and then ad ann input that uses my custom :as type, but I have no idea where to begin.

Does anyone know of any gems that do something like this that I could take a look at the source for? Any suggestions on where to begin?

My gem requires Rails 2.3.x

The source for my custom input looks like this, and I'm including it in an initializer in my application:

module ClassyEnumHelper
  class SemanticFormBuilder < Formtastic::SemanticFormBuilder
    def enum_select_input(method, options)
      enum_class = object.send(method)

      unless enum_class.respond_to? :base_class
        raise "#{method} does not refer to a defined ClassyEnum object" 
      end

      options[:collection] = enum_class.base_class.all_with_name
      options[:selected] = enum_class.to_s

      select_input(method, options)
    end
  end
end

Not sure if any of my other source code would help, but it can be found here http://github.com/beerlington/classy_enum

A: 

I ended up copying a lot from how Formtastic is testing their own helpers. Part of my problem was that I wasn't including all the helpers needed by Formtastic.

The source for my test can be found here:

http://github.com/beerlington/classy_enum/blob/master/spec/spec_helper.rb http://github.com/beerlington/classy_enum/blob/master/spec/classy_enum_helper_spec.rb

Beerlington