Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActionView::TestCase does not initialize helpers contra actual ActionView Behaviour

#5061

Steps to reproduce

# create a new rails app
rails new rails-helper-init && cd rails-helper-init

# create a resource to play with
rails generate scaffold Ninja

# modify the helper so it has an initializer
echo "module NinjasHelper
  attr_accessor   :initialized_count
  def initialize( *args, &block )
    super *args, &block

    @initialized_count    ||= 0
    @initialized_count    +=  1
  end
end" > app/helpers/ninjas_helper.rb

# create a test that checks the helper is initialized
echo "require 'test_helper'

class NinjasHelperTest < ActionView::TestCase
  def test_initialization_count
    assert_equal initialized_count, 1
  end
end" > test/unit/helpers/ninjas_helper_test.rb


# run migrations
rake db:migrate

# run tests
rake test:units

Expected Behaviour

The above helper unit test should pass.

Actual Behaviour

rake test:units
(in /tmp/rails-helper-init)
Loaded suite /home/davidjh/.rvm/gems/ree-1.8.7-2010.01/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
.F
Finished in 0.013791 seconds.

  1) Failure:
test_initialization_count(NinjasHelperTest) [/test/unit/helpers/ninjas_helper_test.rb:5]:
<nil> expected but was
<1>.

2 tests, 2 assertions, 1 failures, 0 errors
rake aborted!

Notes

This is strictly an issue with testing: the view is initialized properly
(presumably because the initializer of the class that mixes it in calls super) when running rails.

Misc.

[davidjh@nyx (; ree-1.8.7-2010.01)] /tmp/rails-helper-init
$ rails --version
Rails 3.0.0.beta4
[davidjh@nyx (; ree-1.8.7-2010.01)] /tmp/rails-helper-init
$ ruby --version
ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.01

Reported by hjdivad · July 7th, 2010 @ 03:16 PM

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. David Chelimsky
  2. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to open
    • Importance changed from to Low

    I was able to reproduce this on Rails 3.0

    I also found the following two tests produce strange output.

    Two additional tests:

      def test_initialization_count_again
        assert_equal 1, initialized_count
      end
    
      def test_initialization_count_after_foo
        foo
        assert_equal 1, initialized_count
      end
    

    Output:

      1) Failure:
    test_initialization_count(NinjasHelperTest) [test/unit/helpers/ninjas_helper_test.rb:5]:
    <1> expected but was
    <nil>.
    
      2) Failure:
    test_initialization_count_after_foo(NinjasHelperTest) [test/unit/helpers/ninjas_helper_test.rb:14]:
    <1> expected but was
    <2>.
    
    3 tests, 3 assertions, 2 failures, 0 errors, 0 skips
    

    Note that the original test fails, however the same test repeated passes and also the method foo has access to @initialized_count (set to 1).

    Could this be the result of some kind of lazy loading? I haven't yet dug into the Rails source yet but I thought I should report my findings if somebody else finds them helpful.

    September 13th, 2010 @ 06:17 AM

  3. David Chelimsky
    David Chelimsky

    @rohit - I'm confused about the foo method in your example. Can you post it (and tell me where it lives)?

    September 13th, 2010 @ 03:02 PM

  4. Repository
  5. Repository
  6. Rohit Arondekar
    Rohit Arondekar

    Just for completeness...

    @David, sorry for not including the module code. The module with the additional foo method is as follows:

    module NinjasHelper
      attr_accessor   :initialized_count
      def initialize( *args, &block )
        super *args, &block
    
        @initialized_count    ||= 0
        @initialized_count    +=  1
      end
    
      def foo
        @initialized_count ||= 0
        @initialized_count += 1
      end
    end
    

    The tests and result of running tests was same as above.

    September 14th, 2010 @ 04:18 AM