我目前正在开发一个动态添加一些功能的gem:
ActiveSupport.on_load(:action_view) do
include CustomFormBuilder::FormHelper
end
此表单构建器具有custom_form_for
方法,该方法调用render
以呈现部分。
在实践中,这很有效。
但是,我正在尝试添加一些rspec测试用例来测试gem,我一直得到:
undefined method 'render' for #<RSpec::ExampleGroup::CustomFormBuilderFormHelper::CustomFormFor
我认为这是因为我的on_load
回调永远不会被击中,尽管我已经尝试过programaticaly调用ActiveSupport.run_load_hooks(:action_view)
,这也没有用。
完整规范代码:
require 'spec_helper'
describe CustomFormBuilder::CustomFormHelper do
describe 'custom_form_for' do
before do
@new_record = CustomFormBuilderSpecHelper::Record.new
end
it "should create custom submit button" do
custom_form_for(@new_record) do |builder|
builder.submit('Submit').should eq 'fail'
end
end
end
end
并且spec_helper:
require 'action_view'
require 'active_model'
require 'custom_form_builder'
include CustomFormBuilder::CustomFormHelper
module CustomFormBuilderSpecHelper
class Record
include ActiveModel::Validations
validates :name, presence: true, inclusion: {in: %w(Name1 Name2 Name3)}
end
end