Testing active admin returns "undefined method `view_factory`" #918

Closed
gabeodess opened this Issue Jan 18, 2012 · 49 comments

Projects

None yet
@gabeodess

I am using Spork, Guard, Capybara, and TestUnit.

When I try to do a simple test:

visit new_admin_user_session_path
fill_in 'admin_user[email]',    with: 'foobar@example.com'
fill_in 'admin_user[password]', with: 'password'
click_button 'Login'

I get the following error on the last line (click_button 'Login'):

ActionView::Template::Error: undefined local variable or method `view_factory' for #<#<Class:0x000001070ccf80>:0x000001070c7648>

Please advise.

@bcavileer

I'm getting the same with Spork, Guard, Capybara, and RSpec.
If I run rake spec:requests it runs ok. It's definately an issue with combining Spork and ActiveAdmin.

I'd really like to see this fixed so I can get back to testing with Spork!

@idrozd
idrozd commented Jan 23, 2012

Same here

@idrozd
idrozd commented Jan 31, 2012

Looks like template missing ActiveAdmin::ViewHelpers when run via spork for some reason.
Couldn't find out which one, cause it'd take few days with my rails knowledge, but prying in actionpack was fun )

Ok folks, this works for me for now:

i've hacked AA's code like this (...lib/active_admin/arbre.rb):

ActionView::Template.register_template_handler :arb, lambda { |template|
    "self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
    # old line is "self.class.send :include, Arbre::Builder; @_helpers = self; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
}
@gabeodess

Works like a charm!

I simply dropped the above code into test_helper.rb in Spork's run-every-time block and everything is running beautifully!

# test/test_helper.rb
Spork.each_run do
  ActionView::Template.register_template_handler :arb, lambda { |template|
    "self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
  }
end

Thanks @idrozd !

@idrozd
idrozd commented Feb 1, 2012

Oh thank you back, that's more convinient.
Offtop: there also should be following lines in each_run

ActiveAdmin.unload!
ActiveAdmin.load!
@apauly
apauly commented Apr 4, 2012

I am getting this exception also in my current development environment.

I just updated my Gemfile to use the current master branch in order to try out the "register_page" feature.

When I just follow the instructions mentioned in docs/10-custom-pages.md, I get this exception on the dashboard:

undefined local variable or method `view_factory' for #<#<Class:0x007ff0110fe548>:0x007ff01a25fac8>

1: insert_tag view_factory.dashboard_page

/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:47:in `method_missing'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/app/views/active_admin/dashboard/index.html.arb:1:in `___sers_alp__rvm_gems_ruby_______p____mri_mygemset_bundler_gems_active_admin_______c______app_views_active_admin_dashboard_index_html_arb___1576345100228987116_70334603780520__3355387509140424254'
actionpack (3.0.10) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.10) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.10) lib/action_view/template.rb:127:in `render'
actionpack (3.0.10) lib/action_view/render/rendering.rb:59:in `block in _render_template'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.10) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
. . .

When I try to access any other page, I get the following exception:

undefined local variable or method `active_admin_application' for #<ActiveAdmin::Views::Pages::Index:0x007ff0110eb1a0>

1: insert_tag renderer_for(:index)

/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:47:in `method_missing'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/views/pages/base.rb:24:in `block in build_active_admin_head'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:96:in `with_current_dom_context'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/views/pages/base.rb:23:in `build_active_admin_head'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/views/pages/base.rb:9:in `build'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:63:in `build_tag'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:77:in `insert_tag'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/lib/active_admin/arbre/builder.rb:41:in `method_missing'
/Users/alp/.rvm/gems/ruby-1.9.2-p290-mri@MYGEMSET/bundler/gems/active_admin-302501c32301/app/views/active_admin/resource/index.html.arb:1:in `___sers_alp__rvm_gems_ruby_______p____mri_mygemset_bundler_gems_active_admin_______c______app_views_active_admin_resource_index_html_arb__1264659700321454308_70334527536000__3355387509140424254'
actionpack (3.0.10) lib/action_view/template.rb:135:in `block in render'
activesupport (3.0.10) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.10) lib/action_view/template.rb:127:in `render'
. . .

I'm using rails 3.0.1 and ruby 1.9.2

This is even happening, when I don't use the "register_page" feature.
Anyone with some advise?

@apauly
apauly commented Apr 5, 2012

Instead of the master branch I'm now using the '0.4.x-stable' branch, which works just fine.

@jpmckinney
Contributor

The monkey-patch to arbre integration mentioned above is no longer necessary.

@jpmckinney jpmckinney closed this Aug 29, 2012
@lukekhamilton

@jpmckinney I just bundle updated to activeadmin (0.5.0), and am getting this error message within my guard/spoke/rpsec setup:

ActiveAdmins Login with valid login information 
Failure/Error: admin_login admin
ActionView::Template::Error:
   undefined local variable or method `view_factory' for :Arbre::Context

All working find just running a rspec spec/ command. I have also removed the monkey-patch from my spec_helper.rb file too.

@jpmckinney
Contributor

Can you bundle update arbre as well?

@lukekhamilton

Arbre was updated with my original project based "bundle update".

-- a/Gemfile.lock
+++ b/Gemfile.lock
activeadmin (0.4.4)

  • activeadmin (0.5.0)
  •  arbre (>= 1.0.1)
    

On Fri, Sep 14, 2012 at 2:44 PM, James McKinney notifications@github.comwrote:

Can you bundle update arbre as well?

โ€”
Reply to this email directly or view it on GitHubhttps://github.com/gregbell/active_admin/issues/918#issuecomment-8551645.

@jpmckinney jpmckinney reopened this Sep 14, 2012
@jpmckinney
Contributor

Ok, re-opening issue.

@jpmckinney
Contributor

Does this solution work? #1285 (comment)

@jpmckinney jpmckinney closed this Sep 17, 2012
@jpmckinney jpmckinney reopened this Sep 17, 2012
@xhh
xhh commented Sep 25, 2012

๐Ÿ‘
I have tried the above suggestion, but got uninitialized constant Arbre::Builder
(same with #1675)

@jpmckinney
Contributor

Which above suggestion? Did you update ActiveAdmin and update Arbre (bundle update arbre)? Did you try removing app/admin/active_admin/view_helpers.rb (if you have that file)?

@xhh
xhh commented Sep 26, 2012

@jpmckinney Yes, I have ActiveAdmin 0.5.0 and Arbre 1.0.1. The problem I met, the solution I tried and the result I got are exactly the same with the reporter of #1675 (undefined local variable or method `active_admin_application' for :Arbre::Context)

@gabeodess

@jpmckinney updating active admin (0.4.3 to 0.5.0) and removing the monkey-patch gives me:

ActionView::Template::Error: undefined local variable or method `view_factory' for :Arbre::Context

My Stack:

rails 3.2.8
activeadmin 0.5.0
arbre (1.0.1)
guard-test 0.5.0
guard-spork 1.2.0
spork-testunit 0.0.8
capybara 1.1.2
@tyrw
tyrw commented Oct 13, 2012

I am also having this problem

ActionView::Template::Error:undefined local variable or method 'active_admin_application' for :Arbre::Context

rails 3.2.7
rspec 2.10.0
rspec-rails 2.10.1
capybara 1.1.2
factory_girl 4.1.0
factory_girl_rails 4.1.0

activeadmin 0.5.0
arbre 1.0.1
guard 1.3.3
guard-rspec 1.2.1
spork 0.9.2

Also I do not have app/admin/active_admin/view_helpers.rb, never did

@pschuegr
pschuegr commented Nov 5, 2012

I dug into this a little bit (not much - active admin is full of magic!) and it looks like none of the ActiveAdmin view helpers (the system ones, not custom ones) are being included when running in drb. Gabeodess's fix helped a little, and I could get more things working by manually injecting view helpers into RSpec but ultimately that's just a hack.

    config.include ActiveAdmin::ViewHelpers, :type => :request
    config.include ActiveAdmin::ViewsHelper, :type => :request
@timdiggins

I'm having the same problem with a new rails 3.2.11 app, active_admin 0.5.1, arbre 1.0.1, spork, capybara, as #1675 which redirects here...

Gabeodess's fix above doesn't work because Arbre::Builder doesn't exist in arbre 1.0.1. I've tried working round lots of alternatives... but don't really understand what's causing the problem

@fuzziness

the same here :-(

@timdiggins

FYI - this problem no longer occurs for me in spork/rpsec, because (I think) of various changes I made for other reasons, so I'll note down the changes I made to spec_helper.rb

Adding, early on:
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)

Then in prefork do
require File.expand_path("../../config/application", FILE)
instead of pulling in the whole of the environment which you actually pull in during Spork.each_run:
require File.expand_path("../../config/environment", FILE)
This increases the lag of individual test runs considerable (as you more has to be done during each_run) but makes class loading much more predictable (You can keep class caching on) and I think is the thing that solved the above issue for me.

@bcavileer

I've run into this issue with Spork and Cucumber w/ Capybara.

None of the fixes mentioned above or in #1675 or #1285 helped me solve this problem.

ActionView::Template::Error: undefined local variable or method `active_admin_application' for :Arbre::Context

If anyone has figured out how to test ActiveAdmin 0.5.1 using Spork please help!

@timdiggins

@bcavileer - did you try the fix I detailed above (which is to delay loading some of the rails environment until each_run) - this has solved my problems in two different projects now.

@fuzziness

it works! :-) but only with guard :-( rake spec call fails...

@timdiggins

@fuzziness - that's weird I've never had the problem outside of spork.

@yevgenko

@red56 thank you for the fix, it works for me too!

@budu
budu commented Mar 8, 2013

Any updates on this issue?

@budu
budu commented Mar 8, 2013

I didn't like the idea of reloading the environment on each run, so I've updated @idrozd code to work with the latest version of Arbre:

Spork.each_run do
  # HACK: Fix Arbre template handler problem when application loaded
  # through Spork.
  handler = -> template {%(
    self.extend ActiveAdmin::ViewHelpers
    arbre_context = Arbre::Context.new(assigns, self) {
      #{template.source}
    }
    self.class.send :include, Arbre::Element::BuilderMethods
    arbre_context.to_s
  )}
  ActionView::Template.register_template_handler :arb, handler
end

I still don't get what is the root cause, but this code is working for me.

@budu
budu commented Mar 8, 2013

Spoke too fast, the above code snippet is just working for the first loaded page inside feature specs, searching for a better fix.

@budu
budu commented Mar 8, 2013

Here's part of the full stack trace I'm getting, what is really weird is that the build_tag method is re-entered multiple times before the exception arise.

ActionView::Template::Error:
  undefined local variable or method `arbre_context' for #<#<Class:0x007f0ef4a01838>:0x007f0ef56a6728>
# /lib/arbre/element/builder_methods.rb:23:in `build_tag'
# /lib/arbre/element/builder_methods.rb:39:in `insert_tag'
# /lib/arbre/element/builder_methods.rb:14:in `form_for'
# /lib/active_admin/filters/forms.rb:75:in `active_admin_filters_form_for'
# /lib/arbre/element.rb:175:in `method_missing'
# /lib/active_admin/filters/resource_extension.rb:115:in `block in filters_sidebar_section'
# /lib/active_admin/views/components/sidebar_section.rb:19:in `instance_eval'
# /lib/active_admin/views/components/sidebar_section.rb:19:in `build_sidebar_content'
# /lib/active_admin/views/components/sidebar_section.rb:12:in `build'
# /lib/arbre/element/builder_methods.rb:30:in `block in build_tag'
# /lib/arbre/context.rb:92:in `with_current_arbre_element'
# /lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
# /lib/arbre/element/builder_methods.rb:26:in `build_tag'
# /lib/arbre/element/builder_methods.rb:39:in `insert_tag'
# /lib/arbre/element/builder_methods.rb:14:in `sidebar_section'
# /lib/active_admin/views/pages/base.rb:117:in `block (2 levels) in build_sidebar'
# /lib/active_admin/views/pages/base.rb:116:in `collect'
# /lib/active_admin/views/pages/base.rb:116:in `block in build_sidebar'
# /lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
@budu
budu commented Mar 11, 2013

I've worked on this issue a bit more and this keep getting stranger, when not running Spork, everything is running fine. I changed Arbre's BuilderMethods#build_tag to output what it is trying to build (its klass argument).

Without Spork I get:

ActiveAdmin::Views::SidebarSection
Arbre::HTML::H3
Arbre::HTML::Div
ActiveAdmin::Views::Footer
Arbre::HTML::P

and when running with it:

Admin::Views::SidebarSection
Arbre::HTML::H3
Arbre::HTML::Div
Arbre::Rails::Forms::FormForProxy

So at some point Arbre is trying to build the filter form_for tag inside an anonymous class which doesn't have the arbre_context method, I really don't understand why or how this happen though. Any idea?

@budu
budu commented Mar 12, 2013

@HatemMahmoud I've already got this line on top of my prefork block, moving it on top of the file doesn't change anything. From my understanding, this line only prevent models from being preloaded so that Spork could reload them on each run, thus it doesn't have anything to do with the issue at hand unless I'm mistaken.

@seanlinsley
Member

@budu, looking at that stack trace and at the code, I noticed that active_admin_filters_form_for is the only method anywhere that calls form_for; everything else uses active_admin_form_for. Perhaps that would fix it?

@budu
budu commented Mar 14, 2013

@Daxter that might be it but I'm not sure what the fix would be though, any idea?

@seanlinsley
Member

I guess I wasn't clear enough. Could you try changing that line locally to use active_admin_form_for?

@budu
budu commented Mar 14, 2013

@Daxter ok, I'll try it later, as I've got rid of Spork in my current project.

@joshuavial

I was able to shave a bit more time off my cucumber setup by putting the following line in Spork.prefork

require File.expand_path(File.dirname(__FILE__) + '/../../config/application')

and this line in Spork.each_run

MYAPP::Application.initialize!
@obromios

Using Spork, in my app, I did not have this problem until I inserted

module ActiveAdmin
    ResourceController.class_eval do
        with_role :admin
      end
end

into the active_admin.rb initializer file. However, I had already inserted

Spork.trap_method(Rails::Application::RoutesReloader, :reload!) 

into my spec_helper.rb file to fix a different arbre problem. Maybe this provides a hint as to the problem.

@gvt
gvt commented Apr 9, 2013

This seems unsolved as of yet. So has everyone given up on testing code that uses ActiveAdmin? Or just given up on ActiveAdmin itself and replaced with custom code.. ? I'd love to hear about your experiences. After spending a day with this and getting nowhere other than confused, I'm left wondering about the state of the ActiveAdmin project.

@gabeodess

@gvt I'm still using version 0.4.3 with the patch mentioned at the top of the thread and it's working for me.

@seanlinsley
Member

@gvt I don't personally bother testing Active Admin from within my projects, but I'm more than willing to fix this.

The closest I think I've gotten to figuring it out was the above discussion with @budu, but he stopped using Spork.

@obromios
obromios commented Apr 9, 2013

I find active admin very useful, but partly as a result of this issue, have decided not to write tests for admin functions, apart from testing access security. At the end of the day, I can always fall back to console until I have fixed any bugs in the admin interface.

@timdiggins

I have tests running fine under spork for two different projects - have had to
sacrifice a second or so of startup speed which is acceptable for me (I
don't auto test). See my comments above for details

BTW in terms of what I test w active admin - I test the things that might
fail or have failed: each view (page) working and then specific custom
actions.

On Tuesday, April 9, 2013, obromios wrote:

I find active admin very useful, but partly as a result of this issue,
have decided not to write tests for admin functions, apart from testing
access security. At the end of the day, I can always fall back to console
until I have fixed any bugs in the admin interface.

โ€”
Reply to this email directly or view it on GitHubhttps://github.com/gregbell/active_admin/issues/918#issuecomment-16094511
.

@demental
demental commented Apr 9, 2013

Basic acceptance tests finally got to work with the latest master commits here and Rails 3.2.11, I'm not really sure how and why, but I'm still using spork and using this in the prefork:

  ENV["RAILS_ENV"] ||= 'test'

  require 'rails/application'

  Spork.trap_method(Rails::Application::RoutesReloader, :reload!)

  # Prevent main application to eager_load in the prefork block (do not load files in autoload_paths)
  Spork.trap_method(Rails::Application, :eager_load!)

  require File.expand_path("../../config/environment", __FILE__)

  # Load all railties files
  Rails.application.railties.all { |r| r.eager_load! }

However, I still use activeAdmin in existing projects but I'm not sure I would use it anymore for new enduser admin interfaces. It's nice for fast admin prototyping but, mainly because some aspects of it involves too much magic, that makes it more difficult to access and test, cannot substitute itself to a made from scratch admin interface.
This may be also because of my lack of knowledge of the AA project itself, I'm using a custom admin generator on some php projects, that has similar philosophy, but as the author I feel more comfortable when I need to dive into the inner guts (which you cannot avoid). I guess this can be the same for some advanced AA users and contributors. So keep on doing the good work, and definitely, ease testing of AA-based projects is a priority IMHO.

@nestegg
nestegg commented Jan 31, 2014

Is this still an issue for anyone?

@kchasel
kchasel commented Mar 13, 2014

@nestegg I am definitely still having this problem. I've got a good number of tests surrounding my active admin code, and after updating to Rails 4 (4.0.2) and activeadmin (1.0.0.pre), as well as spork-rails (4.0.0), all my admin controller specs work fine outside spork, but when running within spork they all fail with the dreaded
undefined local variable or method active_admin_application for #<ActiveAdmin::Views::Pages::Index:0x0000010f157df8>

I've been poking around for about two hours now but despite trying all the aforementioned hacks this is basically beyond me :/

@Fivell Fivell added the help wanted label Jan 29, 2017
@deivid-rodriguez
Contributor

I would close this, spork seems abandoned and this issue is really old.

@timoschilling
Member

I think we can close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment