3

我创建了一个 FormBuilder 扩展:

form.div_field_with_label(:email)

哪个输出:

<div class="field email">
    <label for="user_email">Email</label>
    <input class="input" id="user_email" name="user[email]" type="email" value="">
</div>

如何在我的 rspec 测试中创建或模拟模板对象?

应用程序/助手/form_helper.rb

module FormHelper

  class GroundedFormBuilder < ActionView::Helpers::FormBuilder

    def div_field_with_label(key, &block)

      if block_given?
        content = self.label(key)
        content << block.call(key)
      end
      classes = ['field']
      classes << key.to_s
      if @object.errors[key].size != 0
        classes << 'warning'
        msg = @object.errors.full_message(key, @object.errors[key].join(', '))
        content << @template.content_tag(:p, msg, :class => "error-message warning alert" )
      end
      @template.content_tag(:div, content, :class => classes.join(' ').html_safe)
    end
  end
end

规范/助手/form_helper_spec.rb

require 'spec_helper'

describe FormHelper do

  describe FormHelper::GroundedFormBuilder do

    describe 'div_field_with_label' do

      # how do i create or mock template?
      # template = ?
      let(:resource)  { FactoryGirl.create :user }
      let(:helper)    { FormHelper::GroundedFormBuilder.new(:user, resource, template, {})}
      let(:output)    { helper.div_field_with_label :email }

      it 'wraps input and label' do
        expect(output).to match /<div class="field">/
      end

      it 'creates a label' do
        expect(output).to match /<label for="user[email]">Email/
      end
    end
  end
end
4

3 回答 3

5

Rails 3 或更高版本的更新。

这应该是现在的样子:

require 'spec_helper'

class TestHelper < ActionView::Base; end

describe LabeledFormBuilder do
  describe '#upload_tag' do
    let(:helper) { TestHelper.new }
    let(:resource)  { FactoryGirl.build :user }
    let(:builder) { LabeledFormBuilder.new :user, resource, helper, {}, nil }
    let(:output) do
      builder.upload_tag :avatar, title: "Test upload"
    end
    before { expect(helper).to receive(:render) { "render partial "} }

    it { expect(output).to include "<a href=\"#avatar-upload-modal\" class=\"btn\" data-toggle=\"modal\" role=\"button\">Upload file via new solution</a>" }
  end
end
于 2014-02-27T15:11:03.740 回答
1

我还没有尝试过,但是看看你的GroundedFormBuilder使用方式@template,也许你需要的只是一个扩展的对象ActionView::Helpers::TagHelper

# ...
template = Object.new.extend ActionView::Helpers::TagHelper
# ...

任何TagHelper方法content_tag都应该生成与包含在真实模板中时相同的内容。

于 2013-10-28T06:44:07.400 回答
0

实际上它就像在继承链中的某个地方self使用FormBuilder一样简单。TagHelper

# spec/helper/form_helper_spec.rb
require 'spec_helper'

describe FormHelper do

  describe FormHelper::GroundedFormBuilder do

    describe 'div_field_with_label' do

      # how do i create or mock template?
      # template = ?
      let(:resource)  { FactoryGirl.create :user }
      let(:helper)    { FormHelper::GroundedFormBuilder.new(:user, resource, self, {})}
      let(:output)    {
        helper.div_field_with_label :email do
          helper.email_field(:email, :class => 'input')
        end
      }

      it 'wraps input and label' do
        expect(output).to include '<div class="field email">'
      end

      it 'creates a label' do
        expect(output).to include '<label'
      end
    end
  end
end
于 2013-10-28T07:30:18.237 回答