[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 

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.

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.


More information about the rspec-users mailing list