Output_buffer is null for an auxiliary specification

I am trying to check the html block method inside the rails helper:

def dashboard_widget(header, &proc)
  concat('<div class="dashboard-widget">')
  etc
end

The code works fine in the development environment, but the following test failed:

it "should be created" do
  helper.dashboard_widget('My widget') do 
    "hello world" 
  end.should be_true
end

Using the following stacktrace command:

d:/s/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/text_helper.rb:32:in `concat'
D:/makabu/medved/winvest/master/app/helpers/dashboards_helper.rb:6:in `dashboard_widget'
./spec\helpers\dashboards_helper_spec.rb:13:
d:/s/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.8/lib/spec/example/example_methods.rb:40:in `instance_eva
l'
d:/s/ruby/lib/ruby/gems/1.8/gems/rspec-1.2.8/lib/spec/example/example_methods.rb:40:in `execute'

Please tell me what am I doing wrong?

Thanks.

+3
source share
1 answer

There is an RSpec method for testing helpers that combine output instead of returning it:

output = eval_erb("<% helper.dashboard_widget('My widget') { 'hello world' } %>")
output.should be_happy # meaningful statement goes here

This is the best way to approach the blocks that will have HTML from your views:

output = eval_erb <<-HTML
  <% helper.dashboard_widget('My widget') do %>
    <h1>Widget</h1>
    <p>Hello World, this is my cool widget</p>
  <% end %>
HTML

However, I found a cleaner way to test helpers that use concatenation but don't use HTML inside the block:

before :each do
  helper.output_buffer = ''
end

it "should be awesome"
  helper.dashboard_widget 'My widget' do
    :awesome
  end

  helper.output_buffer.should match(/awesome/)
end

ERB, , .

+8

Source: https://habr.com/ru/post/1718535/


All Articles