Having trouble with helper tests (content_for)

743 views
Skip to first unread message

Petri Wessman

unread,
May 10, 2013, 1:44:56 PM5/10/13
to minites...@googlegroups.com
Hello all, I'm in the process of migrating from rspec to minitest, using minitest-rails, and am having problems with my helper tests. I solved one "missing html_escape" problem by including include ERB::Util, but this other problem has me stumped.

I narrowed it down to "content_for", and for test purposes tried a simple test case. I added the following into my ApplicationHelper:

  def foo
    content_for(:foo) do
      "Hello world"
    end
  end

and in my application_helper_test.rb file I have:

require 'test_helper'
describe ApplicationHelper do

  it "handles content_for" do
    content_for(:foo).must_equal 'Hello world'
  end

end

when I run this, I get:

ApplicationHelper
    ERROR (0:00:00.011) 0001 handles content for
          undefined method `get' for nil:NilClass
        @ /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/actionpack-3.2.13/lib/action_view/helpers/capture_helper.rb:142:in `content_for'
          test/helpers/application_helper_test.rb:9:in `block (2 levels) in <top (required)>'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:36:in `block in run'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:447:in `_run__3596054173487172977__setup__3586964360801127651__callbacks'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
          /home/orava/.rvm/gems/ruby-1.9.3-p392@cornerstone/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run'

If I look in action_view/helpers/capture_helper.rb:142, I see that it's blowing up here:

          @view_flow.get(name)

.i.e. @view_flow is nil. 

Any ideas of what I should do to fix things? All this worked fine in rspec-rails, though there I needed to append helper. to all helper method calls while here they are apparently being included into the test namespace. 

Is there some special secret sauce for running helper tests with minitest-rails, or is it a bug/missing feature in the current implementation?

-Petri

Mike Moore

unread,
May 10, 2013, 6:42:04 PM5/10/13
to minites...@googlegroups.com
What version of rails and ruby and minitest-rails?


--
-- You received this message because you are subscribed to the Google Groups minitest-rails group. To post to this group, send email to minites...@googlegroups.com. To unsubscribe from this group, send email to minitest-rail...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/minitest-rails?hl=en
---
You received this message because you are subscribed to the Google Groups "minitest-rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minitest-rail...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Petri Wessman

unread,
May 11, 2013, 3:09:21 PM5/11/13
to minites...@googlegroups.com
Rails 3.2.13
Ruby 1.9.3p392
minitest-rails 0.9.2

i.e. all should be the most recent stable versions.

Sorry, I should have specified these in my original post.

Petri Wessman

unread,
May 11, 2013, 3:23:04 PM5/11/13
to minites...@googlegroups.com
Also tried a "clean-room" test:

- created a new rails app
- added minitest-rails to Gemfile

- modified ApplicationHelper:

module ApplicationHelper
  def foo
    content_for(:foo) do
      "Hello world"
    end
  end
end

- added helper test test/helpers/application_helper_test.rb:

require 'test_helper'
describe ApplicationHelper do

  it "handles content_for" do
    foo
    content_for(:foo).must_equal 'Hello world'
  end

end

The result is:

  1) Error:
test_0001_handles content_for(ApplicationHelper):
NoMethodError: undefined method `append' for nil:NilClass
    /home/orava/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_view/helpers/capture_helper.rb:139:in `content_for'
    /home/orava/projects/foo/app/helpers/application_helper.rb:4:in `foo'

That's this line in Rails: 

          @view_flow.append(name, content) if content
 
So yeah, doesn't seem to be anything weird in my original app, it's easily repeatable.

Petri Wessman

unread,
May 11, 2013, 3:25:58 PM5/11/13
to minites...@googlegroups.com
(my original post has a small bug, the helper test should first call "foo" before it tests the content_for value. Corrected in my post above.)

Mike Moore

unread,
May 16, 2013, 10:01:37 AM5/16/13
to minites...@googlegroups.com
I'm back from Scotland now. I'll look at this today.

Marco Colli

unread,
Jul 21, 2013, 5:32:57 AM7/21/13
to minites...@googlegroups.com
I have the exact same problem with Rails 4 and MiniTest. Cannot test helper methods containing content_for.

Matt Smith

unread,
Feb 15, 2014, 5:11:18 AM2/15/14
to minites...@googlegroups.com
I know this is an old thread, but I just stumbled upon this.

You need to initialize @view_flow, just like actionview/test/template/capture_helper_test.rb.

Simply adding a setup block like this should solve the problem:
def setup
  @view_flow = ActionView::OutputFlow.new
end

Hope that helps!

Andrew Carpenter

unread,
Oct 3, 2017, 5:46:09 PM10/3/17
to minitest-rails
Worked for me--thanks Matt!
Reply all
Reply to author
Forward
0 new messages