简体   繁体   English

如何使用rspec测试Formtastic自定义输入?

[英]How do I test a Formtastic custom input with rspec?

I have a rubygem that defines a custom SemanticFormBuilder class which adds a new Formtastic input type. 我有一个rubygem定义了一个自定义的SemanticFormBuilder类,它添加了一个新的Formtastic输入类型。 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. 我以为我可以做一些像加载Formtastic,调用semantic_form_for,然后使用我的自定义的广告输入:as类型,但我不知道从哪里开始。

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 我的宝石需要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 不确定我的其他任何源代码是否有帮助,但可以在http://github.com/beerlington/classy_enum找到

Testing your output 测试你的输出

Our team has had success with this approach, which I think we originally borrowed from Formtastic's own tests. 我们的团队在这种方法上取得了成功,我认为我们最初是从Formtastic自己的测试中借鉴的。

First, create a buffer to capture the output you want to test. 首先,创建一个缓冲区来捕获要测试的输出。

# spec/support/spec_output_buffer.rb
class SpecOutputBuffer
  attr_reader :output

  def initialize
    @output = ''.html_safe
  end

  def concat(value)
    @output << value.html_safe
  end
end

Then call semantic_form_for in your test, capturing the output to your buffer. 然后在测试中调用semantic_form_for ,将输出捕获到缓冲区。 Once you've done that, you can test that the output was what you expected. 完成后,您可以测试输出是否符合预期。

Here's an example where I overrode StringInput to add an integer CSS class to inputs for integer model properties. 这是一个例子,我重写StringInput以将integer CSS类添加到整数模型属性的输入。

# spec/inputs/string_input_spec.rb
require 'spec_helper'

describe 'StringInput' do

  # Make view helper methods available, like `semantic_for_for`
  include RSpec::Rails::HelperExampleGroup

  describe "classes for JS hooks" do

    before :all do
      @mothra = Mothra.new
    end

    before :each do
      @buffer = SpecOutputBuffer.new
      @buffer.concat(helper.semantic_form_for(@mothra, :url => '', as: 'monster') do |builder|
        builder.input(:legs).html_safe +
        builder.input(:girth).html_safe
      end)
    end

    it "should put an 'integer' class on integer inputs" do
      @buffer.output.should have_selector('form input#monster_legs.integer')
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM