This project is archived and is in readonly mode.
ActionView::TestCase does not initialize helpers contra actual ActionView Behaviour
Reported by hjdivad | July 7th, 2010 @ 03:16 PM
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
Comments and changes to this ticket
-
David Chelimsky July 7th, 2010 @ 03:37 PM
- Assigned user set to José Valim
Some background at http://github.com/rspec/rspec-rails/issues#issue/120
-
Rohit Arondekar September 13th, 2010 @ 06:17 AM
- 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.
-
David Chelimsky September 13th, 2010 @ 03:02 PM
@rohit - I'm confused about the
foo
method in your example. Can you post it (and tell me where it lives)? -
Repository September 13th, 2010 @ 09:33 PM
- State changed from open to resolved
(from [84d0c30cea243f020aa7437a2f0313a9b3f13ec4]) Allow view helper's #initialize method to be called. [#5061 state:resolved] http://github.com/rails/rails/commit/84d0c30cea243f020aa7437a2f0313...
-
Repository September 13th, 2010 @ 09:33 PM
(from [b5a17d7648116532805c86d4fe16770da1e616f4]) Allow view helper's #initialize method to be called. [#5061 state:resolved] http://github.com/rails/rails/commit/b5a17d7648116532805c86d4fe1677...
-
Rohit Arondekar September 14th, 2010 @ 04:18 AM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Tags
Referenced by
- 5061 ActionView::TestCase does not initialize helpers contra actual ActionView Behaviour (from [84d0c30cea243f020aa7437a2f0313a9b3f13ec4]) Allow v...
- 5061 ActionView::TestCase does not initialize helpers contra actual ActionView Behaviour (from [b5a17d7648116532805c86d4fe16770da1e616f4]) Allow v...