How to setup Cucumber, Guard & Spork with Rails 3.1? - ruby-on-rails-3.1

I'm trying to setup cucumber, rspec, guard and spork. I've got rspec working fine but cucumber crashes with the trace below.
I've try setting this up several times,
rails g cucumber:install --spork
spork cucumber --boostrap
etc... with no luck.
Gems:
rails (3.1.3)
guard (0.10.0)
guard-bundler (0.1.3)
guard-cucumber (0.7.4, 0.7.3)
guard-rails (0.0.3)
guard-rspec (0.6.0, 0.5.2)
guard-spork (0.5.1, 0.5.0)
cucumber (1.1.4)
cucumber-rails (1.2.1)
spork (0.9.0)
Running all features
Using the default profile...
Running tests with args ["--color", "--format", "pretty", "--require", "/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/guard-cucumber-0.7.4/lib/guard/cucumber/notification_formatter.rb", "--format", "Guard::Cucumber::NotificationFormatter", "--out", "/dev/null", "--require", "features", "features", "--format", "pretty", "--strict", "--tags", "~#wip", "features", "--no-profile"]...
Disabling profiles...
Exception encountered: #<RuntimeError: All but one formatter must use --out, only one can print to each stream (or STDOUT)>
backtrace:
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/cucumber-1.1.4/lib/cucumber/cli/configuration.rb:198:in `arrange_formats'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/cucumber-1.1.4/lib/cucumber/cli/configuration.rb:26:in `parse!'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/cucumber-1.1.4/lib/cucumber/cli/main.rb:55:in `configuration'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/cucumber-1.1.4/lib/cucumber/cli/main.rb:63:in `run_drb_client'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/cucumber-1.1.4/lib/cucumber/cli/main.rb:34:in `execute!'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/test_framework/cucumber.rb:24:in `run_tests'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/forker.rb:21:in `block in initialize'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/forker.rb:18:in `fork'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/forker.rb:18:in `initialize'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/matt/.rvm/gems/ruby-1.9.2-p290#alphascore/gems/spork-0.9.0/lib/spork/server.rb:48:in `run'
/Users/matt/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
/Users/matt/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
/Users/matt/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
/Users/matt/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
/Users/matt/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'
Done.

I finally solved this, after playing around with flags passed to cucumber via my guardfile I was able to resolve conflicting flags and not overriding my cucumber.yml flags.
Working Guardfile
guard 'cucumber', :cli => "--drb" 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
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
Conflicting Guardfile
guard 'cucumber', :cli => "--drb --color --format pretty" 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

Related

Test-Kitchen serverspec testing exception on windows

When using chef generate cookbook hello command, the serverspec spec_helper.rb file generated will not work with Test-Kitchen 1.4.1 and windows.
kitchen verify command will throw the following on windows platform:
C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/backend/exec.rb:98:in `spawn': No such file or directory - /bin/sh -c ls\ /etc/arch-release (Errno::ENOENT)
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/backend/exec.rb:98:in `spawn_command'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/backend/exec.rb:13:in `block in run_command'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/backend/exec.rb:133:in `with_env'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/backend/exec.rb:12:in `run_command'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/detect_os.rb:13:in `run_command'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/detect_os/arch.rb:3:in `detect'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/detect_os.rb:5:in `detect'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/os.rb:24:in `block in detect_os'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/os.rb:23:in `each'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/os.rb:23:in `detect_os'
from C:/Users/vagrant/AppData/Local/Temp/verifier/gems/gems/specinfra-2.37.5/lib/specinfra/helper/os.rb:9:in `os'
from C:/Users/vagrant/AppData/Local/Temp/verifier/suites/serverspec/windows_spec.rb:4:in `<top (required)>'
Changing the test-kitchen spec_helper.rb file to test for windows and setting the backend and os accordingly, fixes the issue:
require 'serverspec'
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil?
set :backend, :exec
else
set :backend, :cmd
set :os, family: 'windows'
end
You can then test against different platforms, for example:
require 'spec_helper'
case os[:family]
when 'windows'
describe file('C:\Users\vagrant\foo.txt') do
it { should be_file }
end
when 'darwin' # mac os x
describe file('/user/vagrant/foo.txt') do
it { should be_file }
end
when 'ubuntu'
describe file('/home/vagrant/foo.txt') do
it { should be_file }
end
end

unable to overcome the error PAC::ProgramError from my Rails application

I am new to Ruby. I have written a simple code :
...
pac = PAC.load(pacfilename)
return pac.find('http://www.google.com')
...
I am using gems
gem 'win32-service', '0.8.2', :platforms => :mingw
gem 'pac', "~> 1.0.0"
gem 'therubyracer', '0.11.0beta1', :platforms => :mingw
I am getting this following error :
PAC::ProgramError in Accounts#new
Showing G:/test/app/views/accounts/new.html.erb where line #6 raised:
Cannot call method 'lastIndexOf' of null
Extracted source (around line #6):
3: >
4:
5:
6: <%= getProxyServer %>
7:
8: <%= link_to 'Back', accounts_path %>
The relevant part of trace file
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:28:in rescue in block in call'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:22:inblock in call'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:57:in block in lock'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:55:incall'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:55:in Locker'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:55:inlock'
pac (1.0.0) lib/pac/runtimes/rubyracer.rb:21:in call'
pac (1.0.0) lib/pac/file.rb:18:infind'
app/helpers/accounts_helper.rb:46:in readPACfileData'
app/helpers/accounts_helper.rb:65:inreadPACfile'
app/helpers/accounts_helper.rb:83:in `getProxyServer'
What is the problem? I want to find out the proxyserverIP, if any, programmatically from the application and then use it for accessing websites. Any help?

Error while upgrading from Rails 3.1 to Rails 3.2

Upgrade steps were performed as mentioned in http://guides.rubyonrails.org/3_2_release_notes.html
Right now I'm on Ruby 1.8.7 and Rails 3.1.x (using system ruby). I installed RVM with ruby 1.8.7 and added to rails 3.2 and then I get the following error:
[app]$ rails console
Faraday: you may want to install system_timer for reliable timeouts
$HOME/src/qbol/tapp/config/environment.rb:16:in `add': undefined method `>' for nil:NilClass (NoMethodError)
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:55:in `add'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:61:in `info'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:86
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:80
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `instance_exec'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `run'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:55:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:136:in `initialize!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `send'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `method_missing'
from $HOME/src/app/config/environment.rb:48
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:236:in `load_dependency'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:103:in `require_environment!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/commands.rb:40
from script/rails:6:in `require'
from script/rails:6
This is on my development box. Any idea why this is happening?
EDIT: Below is the environment.rb. The error is happening on the last line App::Application.initialize!
# Load the rails application
require File.expand_path('../application', __FILE__)
module ActiveSupport
class BufferedLogger
def self.current_user
Thread.current[:user]
end
def self.current_user=(user)
Thread.current[:user] = user
end
def add(severity, message = nil, progname = nil, &block)
return if #level > severity
message = (message || (block && block.call) || progname).to_s
level = {
0 => "DEBUG",
1 => "INFO ",
2 => "WARN ",
3 => "ERROR",
4 => "FATAL"
}[severity] || "UNKNOWN"
user=BufferedLogger.current_user
if(!user.nil?)
idstr = "uid:#{user.id}"
if !user.current_app_user.nil?
idstr.concat(", acid: #{user.current_app_user.account_id}")
end
else
idstr=""
end
message = "[%s: %s #{idstr}] %s" %
["#{level} pid: #{$$}", Time.now.strftime("%y-%m-%d %H:%M:%S"), message]
message = "#{message}\n" unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
end
# Initialize the rails application
App::Application.initialize!
The error is happening inside ActiveSupport::BufferedLogger#add method call and you appear to be monkey-patching this class. The internals of ActiveSupport::BufferedLogger have likely changing between the versions of Rails you're using.
Try removing all of the ActiveSupport::BufferedLogger code from your environment.rb to work past this error and get your app running. Then, if you still need the monkeypatch (don't know why), you'd have to rewrite it on top of the newer version of the class.

Spork.prefork is loading app/models/*

I can not figure out how to get spork not to load all of my app models. Testing changes to my models is greatly slowed down as I am unable to use spork to help. This is what I get when I debug what spork is loading:
- Spork Diagnosis -
-- Summary --
app/models/account.rb
app/models/admin.rb
app/models/affiliate.rb
app/models/app.rb
app/models/application_server.rb
app/models/domain_record.rb
app/models/domain_zone.rb
app/models/event.rb
app/models/oid.rb
app/models/user.rb
config/application.rb
config/boot.rb
config/environment.rb
config/environments/test.rb
config/initializers/api_conversions.rb
config/initializers/backtrace_silencers.rb
config/initializers/compass.rb
config/initializers/devise.rb
config/initializers/hoptoad.rb
config/initializers/inflections.rb
config/initializers/mime_types.rb
config/initializers/rspec_generator.rb
config/initializers/secret_token.rb
config/initializers/session_store.rb
config/routes.rb
lib/application_server_api.rb
lib/oid_helper.rb
lib/soa_record.rb
spec/spec_helper.rb
-- Detail --
--- app/models/account.rb ---
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:54:in `load_model'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:17:in `block (2 levels) in load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:16:in `block in load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `block in each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/paths.rb:102:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:15:in `load_models'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/railtie.rb:88:in `block (2 levels) in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_prepare_callbacks'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/callbacks.rb:40:in `initialize'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `new'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:33:in `build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `block in build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `inject'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/actionpack-3.0.3/lib/action_dispatch/middleware/stack.rb:79:in `build'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:162:in `app'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application/finisher.rb:35:in `block in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `block in run_initializers'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
config/environment.rb:8:in `'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `block in require'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
spec/spec_helper.rb:9:in `block in '
/Users/sean/.rvm/gems/ruby-1.9.2-p136/gems/spork-0.9.0.rc3/lib/spork.rb:23:in `prefork'
spec/spec_helper.rb:4:in `'
update: my spec_helper.rb
require 'rubygems'
require 'spork'
ENV["RAILS_ENV"] = "test"
Spork.prefork do
require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
require 'rspec/rails'
end
Spork.each_run do
# Hub::Application.reload_routes!
end
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
If I move require File.expand_path(File.dirname(__FILE__) + '/../config/environment') out of the prefork, my models are not preloaded, but as you might expect it takes much longer to run my tests.
Any ideas on how to get spork to not load models on a Rails 3 app? I have tried using spork 0.8.4, master branch, and currently 0.9.0.rc3. Does anyone have any ideas how I can avoid having the models preloaded?
After much time spinning my wheels I finally have something that is acceptable:
Spork.prefork do
require "rails/mongoid"
Spork.trap_class_method(Rails::Mongoid, :load_models)
# The following does not work correctly with Devise's routes that load the User model.
# Results in NameError unintitialized *
# :reload_routes! triggers :devise_for which loads and caches the User class.
# https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
# require "rails/application"
# Spork.trap_method(Rails::Application, :reload_routes!)
require 'factory_girl_rails'
Spork.trap_class_method(Factory, :find_definitions)
require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
end
The problem was mostly due to Rails::Mongoid#load_models. After inspecting the debug output of spork and the backtrace of how things are loaded, some clues are provided as to what is loading the models. This page goes into a bit more detail https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu, but I do not find the :reload_routes! helping with Devise forcing the User class to be preloaded.
My specs are running much faster now. If only I could get my User model to not be cached I would be in bliss, but until then, I shall be mostly satisfied.
I have the following setup in both RSpec and Cucumber on Rails 3.1 and models are refreshing:
Set factory_girls_rails require => false in your Gemfile
gem 'factory_girl_rails', :require => false
Replace the Spork.prefork and Spork.each_run blocks with the following code.
if Spork.using_spork?
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
Spork.prefork do
end
Spork.each_run do
require 'factory_girl_rails'
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
# reload routes
Rails.application.reload_routes!
end
end
The following solution suggested here works for me. It also reloads the User model.
# env.rb
Spork.each_run do
require 'factory_girl_rails'
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end
This solution works for me (from http://my.rails-royce.org/2011/03/17/rspec-2-and-fixtures/)
Spork.prefork do
require 'rails/application'
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
Spork.trap_method(Rails::Application, :eager_load!)
require File.expand_path("../../config/environment", __FILE__)
Rails.application.railties.all { |r| r.eager_load! }
require 'rspec/rails'
RSpec.configure do |config|
...
end
end
Note that doing a "load model" in each_run is only a partial solution, this will re-evaluate the model code, but for instance after removing a model method it will still exist in memory until you restart Spork.

Rspec2 issue with Rcov

In my Rakefile, I have a task defined like this:
namespace :test do
desc "Run all specs."
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
t.verbose = false
end
RSpec::Core::RakeTask.new(:coverage) do |t|
t.rcov = true
t.rcov_opts = %q[--exclude "spec"]
t.verbose = true
end
end
When running test:coverage, I get this:
./spec/foo_spec.rb:3: undefined method `describe' for main:Object (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/rcov-0.9.9/bin/rcov:516:in `load'
from /Library/Ruby/Gems/1.8/gems/rcov-0.9.9/bin/rcov:516
from /usr/bin/rcov:19:in `load'
from /usr/bin/rcov:19
rake aborted!
ruby -S rcov -Ispec:lib --exclude "spec" "./spec/foo_spec.rb" failed
Below my gem list:
diff-lcs (1.1.2)
rake (0.8.7)
rcov (0.9.9)
rspec (2.3.0)
rspec-core (2.3.1)
rspec-expectations (2.3.0)
rspec-mocks (2.3.0)
Any idea? Thanks in advance.
The solution, from David Chelimsky:
http://rubyforge.org/pipermail/rspec-users/2010-December/019077.html
require "rspec"
Cheers.

Resources