Jason Yost
Jason Yost

Reputation: 4937

Rspec 3 view spec undefined method for custom helper

I have a custom form builder method in a helper module.

module MediaHelper
  class CustomFormBuilder < ActionView::Helpers::FormBuilder
    ... 
  end

 def custom_form_for(data, *args, &proc)
  options = args.extract_options!
  form_for(data, *(args << options.merge(builder:
  MediaHelper::CustomFormBuilder)), &proc)
 end
end

This works in the view with <= custom_form_for(media) do |f| %>

However it breaks the view spec in RSpec 3, and I receive the error message

ActionView::Template::Error: undefined method custom_form_for' for #<#<Class:0x007fea32454ce0>:0x007fea34064f98>

on render

Is it possible to include the helper?

Upvotes: 0

Views: 881

Answers (1)

Jason Yost
Jason Yost

Reputation: 4937

A custom helper can be included in a view spec with helper(<module>)

In the view spec in my case:

RSpec.describe 'media_items/new', type: :view do
    helper(MediaHelper)
end

When render is called in the spec, it correctly renders the view with <= custom_form_for(media) do |f| %>

Upvotes: 3

Related Questions