Spork "undefined `model_name` for Class:Class`" with Spork 0.9.0rc9, rails 3.1, active_admin #153

Open
andreashappe opened this Issue Oct 25, 2011 · 10 comments

Comments

Projects
None yet
5 participants

Hi,
when running a simple testcase with rspec a reference to an active_admin conroller does not work:

| visit new_admin_user_path

leads to the following exception:

| the admin countroller should check for existence of job
| Failure/Error: visit(new_admin_user_path)
| ActionView::Template::Error:
| undefined method model_name' for Class:Class | # ./spec/requests/admin_create_users_spec.rb:11:inblock (2 levels) in <top (required)>'

First I thought that some modul loading code got mangled but after some tinkering around I've found out that "rake spec" actually work without an error.

Any idea? Do you need additional information? I'd really like to use spork again.

cheers, Andreas

I also get a similar error whenever I try to visit any Active Admin route with cucumber.

For example, my scenario for viewing the admin dashboard fails with an error:

Scenario: Super Admins can access the Surveys in the admin area # features/manage_surveys.feature:12
    Given I am a logged in super_admin                            # features/step_definitions/login_steps.rb:31
    When I am in the admin backend                                # features/step_definitions/common_steps.rb:22
undefined local variable or method `view_factory' for #<#<Class:0x007feb77d20b30>:0x007feb77c6f628> (ActionView::Template::Error)

Cucumber Feature:

@surveys
Feature: Manage Survey Resources
  In order to allow users to evaluate their stress
  As a Super Admin
  I want to be able to manage surveys, questions, and answers

  @super_admin @focus
  Scenario: Super Admins can access the Surveys in the admin area
    Given I am a logged in super_admin
    When I am in the admin backend
    Then I should see "Surveys"

Steps:

When /^I am in the admin backend$/ do
  visit admin_dashboard_path
end

Like the original poster I thought maybe something just wasn't loading correctly so I tried adding require 'active_admin' to the Spork.prefork block in my features/support/env.rb file, including ActiveAdmin::ViewHelpers and pretty much any other thing google search results came up with but nothing works other than disabling Spork (sad).

Ok, my above comment was probably premature. More debugging pointed to a problem with guard or at least guard-cucumber and using a custom cucumber profile. My cucumber tests started working again when using the default :cli options .

For example, my buggy guard cucumber block and cucumber profile:

# Guardfile
guard 'cucumber', :cli => '--profile guard-verbose', :all_on_start => false, :all_after_pass => :false do
  watch(%r{^features/.+\.feature$})
  watch(%r{^features/support/.+$})                      { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end
# config/cucumber.yml
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: --drb <%= std_opts %> features
wip: --drb --tags @wip:3 --wip features
rerun: --drb <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
guard: --drb --color --format progress --strict --tags ~@wip
guard-verbose: --drb --color --format pretty --strict --tags ~@wip

Non-buggy guard cucumber block:

guard 'cucumber', :cli => '--no-profile --color --format pretty --strict --tags ~@wip', :all_on_start => false, :all_after_pass => :false do
  watch(%r{^features/.+\.feature$})
  watch(%r{^features/support/.+$})                      { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end

ivanoats commented Dec 8, 2011

I'm getting a similar error. It only seems to happen on the second run after starting guard, and then saving a rails admin request spec. I'm using the standard Guardfile for cucumber, spec, and spork:

https://gist.github.com/1445444

@ghost

ghost commented Jan 14, 2012

Hi @grafikchaos, how did you resolve the problem? I'm having the same issue with spork and cucumber. I didn't change my profile at all. Can you please post your env.rb? Thanks.

Here's a gist of my env.rb file - https://gist.github.com/1622609 - although I don't think that was the final solution to get mine working.

I had previously started up Cucumber/Spork using the wrong camel-casing for my project folder (oxychallenge instead of OxyChallenge) and no matter how many times I tried to debug it would never work. I remember reading a github issue comment on one of the libraries (sorry I can't find it now) that suggested doing something silly like restart your computer to clear the DRb cache (if I'm remembering correctly).

Long story short is that restarting my computer and navigating to my project folder with the proper camel-casing (or at least match the casing of your project folder exactly) and starting up Cucumber + Spork resulted in everything running smooth again.

@ghost

ghost commented Jan 17, 2012

My env.rb is almost the same. What do you mean by "started up Cucumber/Spork using the wrong camel-casing for my project folder"?

My project folder is named OxyChallenge and I had previously navigated to the folder using a command like cd ~/code/oxychallenge and ran the bundle exec guard command and that's when my problems started and the wild goose chase started.

So every time I went back to the project using the correct name of the folder (i.e., cd ~/code/OxyChallenge ) and starting guard I would encounter the same issue. I finally found a comment on an issue that suggested that the DRb server may have cached data that can only be cleared by power cycling my machine. I tried this suggestion and it worked for me in that no more errors appeared, tests were re-run when watched files were touched and all was right in my development world again.

I'm not sure if that helps you or not, but that was my experience.

driv3r commented Jan 20, 2012

Same here, with Spree, db cleaner, guard-rspec/spork and RSpec.

ActionView::Template::Error:
       undefined method `set_locale_path' for #<#<Class:0x00000005075be8>:0x000000056483e0>

It seems that he does not load ApplicationHelper in which is this method. I tried to go with clean config, reinstalling spork/guards with no result.

How are you loading in the ApplicationHelper? I normally tell the ApplicationController to load all the helpers all the time:

# ApplicationController
...
helper :all
...

driv3r commented Jan 20, 2012

I'm not loading it at all in controller, and rspec without spork goes without any problems with that. Btw. aren't Rails loading all helpers by default by now?

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