We've been going down this route:

http://pastie.caboo.se/165265

We use mocha for our mocking and originally we mocked out the form builder, but the specs felt crufty. We then converted to instantiating the FormBuilder and passing it to partials that needed it.

For the views that send in the FormBuilder, we use the #with passing a block and assert the "is_a?" inside (I'm still not too keen on using the param helper/matchers). This looks a bit more bulky (and is kinda ugly in having assertions in a setup) but it really greased the wheels for us.

We ended up preferring to assert the presence of html vs expectations on a FormBuilder stub, and it made the view specs feel a bit more natural.

One last note: we hacked the #render in rspec_on_rails to output the contents of a partial (which is why the template stubbing the render returns "form partial") and we assert the presence of that text in the response. Helps catch those rascally <% %> instead of <%= %>. I've been meaning to form together a patch and submit a proper extension, but it's as time allows.

Perhaps a bit more than you were asking for, but I hope this helps

-Zach


On Mar 13, 2008, at 1:25 PM, Jonathan Linowes wrote:

Thank you!

for the record, I refactored my code so the view does a form_for, and
any partials that render fields with the form builder does its own
fields_for. This makes the design cleaner and more testable. However,
I'm not sure if there's any performance cost in doing it this way
instead of passing the form builder around, anyone?

On Mar 13, 2008, at 5:05 AM, David Chelimsky wrote:

On Wed, Mar 12, 2008 at 10:19 PM, Jonathan Linowes
<[EMAIL PROTECTED]> wrote:

On Mar 12, 2008, at 5:47 PM, David Chelimsky wrote:

On Wed, Mar 12, 2008 at 9:43 PM, Zach Dennis
<[EMAIL PROTECTED]> wrote:
You can use mocha parameter matching to match on "anything" where
your
form builder would be passed in. You could also use Mocha's
"kind_of"
parameter matcher to ensure that what you expect is a FormBuilder
object.

 Another way to do this is to not pass in your form builder,
but the
object needed, and then use fields_for inside the partial itself.
This
works well in some scenarios.

You could also use rspec's parameter matching, which also supports
"anything" but not "kind_of" ;)

how would i use that in stub_render? eg if my view has
       <%= render :partial => 'pages/foo', :layout =>
'bar', :locals =>
{ :f => f } %>

what's the stub?

As of 1.1.3:

 stub_render(:partial => 'pages/foo', :layout => 'bar', :locals =>
{:f => })

There is now, in trunk, hash_including matcher, so you *should* (I
haven't tried yet) be able to do this:

 stub_render(hash_including(:locals => {:f => }))

That keeps it a bit less brittle, as the previous version will fail if
you add anything else to the actual render call this won't fail.

Cheers,
David





_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to