[rspec-users] Specifying custom rails FormBuilders

Jay Levitt lists-rspec at shopwatch.org
Mon Aug 6 12:42:07 EDT 2007


David Chelimsky wrote:
> On 8/6/07, Jay Levitt <lists-rspec at shopwatch.org> wrote:
>> I want to write a spec for a custom form builder, but I'm not really
>> sure where it should live or how to include the right rspec_on_rails
>> contexts.  Ideally, I should be able to write a spec like:
>>
>>
>> describe LabelledBuilder do
>>
>>   it "should let me create a text field" do
>>     object = mock(object)
>>     object.stub!(:attribute).and_return("Test String")
>>
>>     render :inline =>
>>       "<% form_for(@object, :builder => LabelledBuilder) do |f| -%>
>>         <%= f.text_field :attribute %>
>>        <% end %>"
>>
>>     response.should have_tag("label")
>>   end
>> end
>>
>>
>> but I'm not having much luck.
> 
> How so? What error do you get from that?

Well, if I put it in spec/helpers, where I expected it to work, I get:

NoMethodError in 'LabelledBuilder should let me create a text field'
undefined method `render' for [Dynamically generated class for RSpec 
example]:#<Class:0x35e4d38>
./spec/helpers/labelled_builder_spec.rb:15:

If I instead put it in spec/views, I get:

Exception in 'LabelledBuilder should let me create a text field'
Unhandled render type in view spec.
/Users/jay/Documents/eclipse/mediajoint/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:133:in 
`subject_of_render'
/Users/jay/Documents/eclipse/mediajoint/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:118:in 
`derived_controller_name'
/Users/jay/Documents/eclipse/mediajoint/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:114:in 
`base_view_path'
/Users/jay/Documents/eclipse/mediajoint/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:110:in 
`set_base_view_path'
/Users/jay/Documents/eclipse/mediajoint/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:27:in 
`render'
./spec/views/labelled_builder_spec.rb:15:

which makes sense, since subject_of_render doesn't seem to handle inline 
rendering...  but of course, if I move it out into a file and render it 
there, I have to make one fake view per test case.

Jay



More information about the rspec-users mailing list