[rspec-users] How do I write specs for Rails 3 view block helpers that use #with_output_buffer(&block) ?

Kristian Mandrup kmandrup.github at gmail.com
Mon Aug 23 15:41:25 EDT 2010


I packed it up into a useful reusable gem ;)

http://github.com/kristianmandrup/rspec-action_view

Only these dependencies :)

require 'rspec'
require 'action_view'
require 'active_support/railtie'
require 'action_view/template/handlers/erb'


Now allows for this DSL

describe "My ViewHelpers" do
  it "should render content as expected" do
    setup_action_view do
      tests MyViewHelper, MyOtherViewHelper
    end

    with_action_view do |view|
      view.tab_for('kristian') { 'hello' }.should match /kristian/
      view.hello('david') { 'hello' }.should match /david/

      with_action_view do |view|
        view.with_template(%{
          <%= tab_for('kristian') { 'hello' } %>
        }).should match /hello/
    end
  end
end

Enjoy!

On Aug 23, 8:51 pm, Kristian Mandrup <kmandrup.git... at gmail.com>
wrote:
> I have finally come up with a workable solution
>
> http://gist.github.com/545866
>
> module MyViewHelper
>   def tab_for(clazz, &block)
>     content = with_output_buffer(&block)
>     content_tag :li, content, :class => clazz
>   end
> end
>
> module MyOtherViewHelper
> ...
> end
>
> describe "do it" do
>   it "works" do
>     ActionViewTester.tests MyViewHelper, MyOtherViewHelper
>     ActionViewTester.new do |helper|
>       helper.tab_for('kristian') { 'hello' }.should match /kristian/
>       helper.hello('david') { 'hello' }.should match /david/
>     end
>   end
> end
>
> On Aug 23, 4:04 pm, Kristian Mandrup <kmandrup.git... at gmail.com>
> wrote:
>
> > I found this:http://stackoverflow.com/questions/197164/how-do-i-test-rails-block-h...
>
> >   it 'should do something' do
> >     helper.some_block_helper { the_block_code }.should XXXX
> >   end
>
> > but not sure how to use it
>
> > I have a module which is extended on top of ActiveView::Base
>
> > module MyViewExt
> >     def area(clazz, &block)
> >       content = with_output_buffer(&block)
> >       content_tag :div, content, :class => clazz
> >     end
> > end
>
> > How would I go about testing this in isolation, especially that the
> > effect of using #with_output_buffer(&block)
> > is as expected (especially when I have nested calls of view helpers
> > using this approach!).
>
> > Thanks.
> > _______________________________________________
> > rspec-users mailing list
> > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list