I'm trying to tests my puppet modules, but I get an error message that it can't find class.
I've written a couple of internally used Puppet modules; now that I have my head around puppet, I want to write tests. Since I ran into issues, I decided to essentially start over with a new clean module to figure out what I need to do.
$puppet module generate wet-splunk
<<snip interactive questions>>
$cd splunk
$rake spec
<path snip>ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Class[splunk]
Failure/Error: it { should contain_class('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:5:in `block (3 levels) in <top (required)>'
Finished in 0.21712 seconds (files took 3.14 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:5 # splunk with defaults for all parameters should contain Class[splunk]
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color failed
Checking the files though I think they contain what they should:
manifests/init.pp
# <snip documentation boilterplate>
class splunk {
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_class('splunk') }
end
end
The Could not find class ::splunk is the part that has me confused.
The way I read the spec file is it looking inside init for class{'splunk': ... } That could be me misreading it as it seems odd they would start with a broken spec file, but just in case I tried testing for package instead.
manifests/init.pp
class splunk {
package{'splunk':
ensure => 'present',
}
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_package('splunk') }
end
end
I still get a similar error message:
$ rake spec
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Package[splunk]
Failure/Error: it { should contain_package('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.21168 seconds (files took 3.17 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:6 # splunk with defaults for all parameters should contain Package[splunk]
Sorry to ask such a basic question; I really appreciate your help. I'd really love links to good tutorials on testing Puppet code; I'd love to use Vagrant to have some confidence my code is actually going to do what I want it to.
Update:
I believe I have followed #Peter_Souter's suggestions; I still get the error that it can't find the class. I'm posting the contents of the files referenced to try and validate I did the right thing and in case there are other errors.
$ cat .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
$ cat spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
$ cat Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
desc "Validate manifests, templates, and ruby files"
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
$ cat Gemfile
source 'https://rubygems.org'
puppetversion = ENV.key?('PUPPET_VERSION') ? "#{ENV['PUPPET_VERSION']}" : ['>= 3.3']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.8.2'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
The way that rspec-puppet simulates the catalog for testing, it makes a fixtures directory with the required modules in it. So right now it's running it on an empty directory so it can't find your module. We generally fix that with the puppetlabs-spec-helper which makes a simlink to your current repository in that fixtures directory.
I'd recommend adding the following into your files:
# .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
# spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
# Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
# Gemfile
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
Can you try that and tell me if it works for you?
If somebody stumble around this Thread as I do. The for me correct answer is given in:
Why do I get puppet-rspec 'class does not exist' when it does?
means you have to do:
puppet module generate foo-bar
and then to fix this issue
bundle exec rspec-puppet-init
It's not nice but better then handling with hidden dot files.
I want to use guard to run my bacon tests, my Gemfile looks like:
source 'https://rubygems.org'
gem 'sinatra'
gem 'sidekiq'
gem 'slim'
gem 'puma'
gem 'nokogiri'
gem 'httparty'
group :test, :development do
gem 'guard'
gem 'bacon'
gem 'guard-bacon'
gem 'libnotify'
gem 'rb-inotify'
end
My Guardfile looks like
# parameters:
# output => the formatted to use
# backtrace => number of lines, nil = everything
guard 'bacon', :output => "BetterOutput", :backtrace => 4 do
watch(%r{^lib/(.+)\.rb$}) { |m| "specs/lib/#{m[1]}_spec.rb" }
watch(%r{specs/.+\.rb$})
end
When I run guard the following happens
$ guard
Bacon: Using output BetterOutput.
Bacon: Limiting backtrace to 4 lines.
09:02:05 - INFO - Guard uses Libnotify to send notifications.
09:02:05 - INFO - Guard uses TerminalTitle to send notifications.
09:02:05 - INFO - Guard is now watching at '/home/martin/code/jse-api'
Guard::Bacon started.
[1] guard(main)> %
$
It seems to load everything, get to the guard prompt and exit.
I have no idea why?
The issue seems to the version of guard required by guard-bacon 1.1.0
If you force it to the latest version of guard
gem 'guard', '>= 1.8.0'
It falls back to a older version of guard-bacon 1.0.5 and everything works.
I am using WickedPDF, and I have basically two gems that include the binaries:
gem "wkhtmltopdf-heroku", "1.0.0"
gem "wkhtmltopdf-binary", "0.9.5.3"
The first one is supposed to be just for production, and the second one for development. The deployment to Heroku doesn't work if I have my Gemfile like:
group :development do
gem "wkhtmltopdf-binary", "0.9.5.3"
end
group :production do
gem "wkhtmltopdf-heroku", "1.0.0"
end
And it doesn't work either if I have it like:
group :production do
gem "wkhtmltopdf-heroku", "1.0.0"
end
It just works if I have it without groups. Just like:
gem "wkhtmltopdf-heroku", "1.0.0"
The error that I get is:
RuntimeError: Location of wkhtmltopdf unknown
Why would this happen? Why Heroku is not using the production group?
WickedPdf tries to figure out where the wkhtmltopdf binary lives, but can have a hard time on some systems (particularly shared servers).
You probably have to set it manually in an initializer something like this:
bin_location = case Rails.env
when 'production' then "/wherever/your/binary/is/bin/wkhtmltopdf"
when 'development' then "/local/path/to/wkthmltopdf"
else `which wkhtmltopdf`
end
WickedPdf.config = { :exe_path => bin_location }
I am trying to do a simple test for my model Course, I have wrote this factory:
FactoryGirl.define do
factory :course do
name 'How to be happy ?'
end
end
the course_spec.rb:
require "rspec"
require 'factory_girl_rails'
describe "When a course is created" do
it "can't be deleted if any student is enrolled to it" do
FactoryGirl.find_definitions
course = FactoryGirl.build(:course)
student= Student.create!
course.students << student
course.destroy
course.name.should !=nil
end
end
but, I reach this line
course = FactoryGirl.build(:course)
I get the error:
FactoryGirl::DuplicateDefinitionError: Factory already registered: course
if I comment the :course definition in the factory, I get:
NameError: uninitialized constant Course
Any idea please ?
here is my Gem Envioronment:
RubyGems Environment:
RUBYGEMS VERSION: 1.8.10
RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i686-linux]
INSTALLATION DIRECTORY: /home/sam/.rvm/gems/ruby-1.9.3-p0
RUBY EXECUTABLE: /home/sam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
EXECUTABLE DIRECTORY: /home/sam/.rvm/gems/ruby-1.9.3-p0/bin
RUBYGEMS PLATFORMS:
ruby
x86-linux
GEM PATHS:
/home/sam/.rvm/gems/ruby-1.9.3-p0
/home/sam/.rvm/gems/ruby-1.9.3-p0#global
GEM CONFIGURATION:
:update_sources => true
:verbose => true
:benchmark => false
:backtrace => false
:bulk_threshold => 1000
REMOTE SOURCES:
http://rubygems.org/
----------------------
IDE: JetBrains RubyMine (EAP) RM-112.291, build #RM-112.291
OS: Linux 3.0.0-12-generic[i386]
Java: 1.6.0_23-b23
RubyMine SDK Environment:
Sdk: RVM: ruby-1.9.3-p0
Sdk Version: ver.1.9.3p0 ( revision 33570) p0
Ruby Interpreter: /home/sam/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
RVM Sdk: yes, gemset:[default]
RVM Home: /home/sam/.rvm
Sdk Language Level: 1.9
Sdk Load Path:
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/i686-linux
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby/1.9.1
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby/1.9.1/i686-linux
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/vendor_ruby
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1
/home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/i686-linux
/home/sam/rubyMine4beta/rubystubs19
Sdk Gem paths:
file:///home/sam/.rvm/gems/ruby-1.9.3-p0/bundler/gems
file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems
file:///home/sam/.rvm/gems/ruby-1.9.3-p0#global/gems
Gems used for 'hope':
ffi (1.0.11, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/ffi-1.0.11)
sass (3.1.12, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sass-3.1.12)
coffee-script-source (1.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-script-source-1.2.0)
mail (2.3.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/mail-2.3.0)
activesupport (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.0)
i18n (0.6.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/i18n-0.6.0)
uglifier (1.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/uglifier-1.2.1)
sprockets (2.0.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sprockets-2.0.3)
foreigner (1.1.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/foreigner-1.1.1)
treetop (1.4.10, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/treetop-1.4.10)
haml (3.1.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/haml-3.1.4)
ansi (1.4.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/ansi-1.4.1)
mime-types (1.17.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/mime-types-1.17.2)
rack (1.3.6, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.3.6)
devise (1.5.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/devise-1.5.2)
warden (1.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/warden-1.1.0)
diff-lcs (1.1.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/diff-lcs-1.1.3)
activeresource (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activeresource-3.1.0)
sass-rails (3.1.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sass-rails-3.1.5)
therubyracer (0.9.9, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/therubyracer-0.9.9)
rspec-expectations (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-expectations-2.7.0)
coffee-rails (3.1.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-rails-3.1.1)
activemodel (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.1.0)
simple_form (1.5.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/simple_form-1.5.2)
bundler (1.0.21, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/bundler-1.0.21)
thor (0.14.6, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/thor-0.14.6)
activerecord (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.0)
json (1.6.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/json-1.6.4)
coffee-script (2.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/coffee-script-2.2.0)
execjs (1.2.13, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/execjs-1.2.13)
turn (0.8.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/turn-0.8.3)
rake (0.9.2.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2)
kaminari (0.13.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/kaminari-0.13.0)
rdoc (3.12, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rdoc-3.12)
rspec (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-2.7.0)
actionpack (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.1.0)
rspec-rails (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-rails-2.7.0)
guard (0.10.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/guard-0.10.0)
cocoon (1.0.15, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/cocoon-1.0.15)
libv8 (3.3.10.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/libv8-3.3.10.4-x86-linux)
rspec-core (2.7.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-core-2.7.1)
jquery-rails (1.0.19, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/jquery-rails-1.0.19)
rails (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rails-3.1.0)
rack-ssl (1.3.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-ssl-1.3.2)
arel (2.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/arel-2.2.1)
erubis (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/erubis-2.7.0)
rb-fsevent (0.4.3.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rb-fsevent-0.4.3.1)
builder (3.0.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/builder-3.0.0)
orm_adapter (0.0.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/orm_adapter-0.0.5)
rack-cache (1.0.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-cache-1.0.3)
multi_json (1.0.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/multi_json-1.0.4)
spork (0.9.0.rc9, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/spork-0.9.0.rc9)
tilt (1.3.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/tilt-1.3.3)
rack-mount (0.8.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-mount-0.8.3)
tzinfo (0.3.31, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/tzinfo-0.3.31)
validate_url (0.2.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/validate_url-0.2.0)
sqlite3 (1.3.5, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/sqlite3-1.3.5)
rspec-mocks (2.7.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rspec-mocks-2.7.0)
polyglot (0.3.3, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/polyglot-0.3.3)
railties (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.1.0)
hike (1.2.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/hike-1.2.1)
guard-spork (0.5.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/guard-spork-0.5.0)
factory_girl_rails (1.4.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl_rails-1.4.0)
factory_girl (2.3.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/factory_girl-2.3.2)
will_paginate (3.0.2, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/will_paginate-3.0.2)
rack-test (0.6.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/rack-test-0.6.1)
client_side_validations (3.1.4, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/client_side_validations-3.1.4)
actionmailer (3.1.0, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.1.0)
bcrypt-ruby (3.0.1, file:///home/sam/.rvm/gems/ruby-1.9.3-p0/gems/bcrypt-ruby-3.0.1)
EDIT
The spec_helper.rb file:
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.factories.clear
# either this if you have multiple files under 'spec/factories',
# or just load the single file, such as 'spec/factories.rb'
Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file|
load "#{file}"
end
end
Did you try to remove this line ?
FactoryGirl.find_definitions
I am using Factory Girl (with the factory_girl_rails gem) & Shoulda with a 3.1 project and didn't have to explicitly load definitions.
All you should need to do is require 'rspec_factory_girl' and then the definitions.
Generally if you put it into the spec_helper.rb, it will require all the files in the spec/ directory, which means if you have your factories defined for example in spec/factories.rb, they will get loaded automatically.
That however requires you to add require 'spec_helper' at the top of your spec file, which seems you're not doing.
If you don't want to use the spec_helper.rb file this way, then just manually require the definition, such as require 'factories'.
One little trick if you're using something like spork and need to reload your factories manually before each run
FactoryGirl.factories.clear
# either this if you have multiple files under 'spec/factories',
# or just load the single file, such as 'spec/factories.rb'
Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file|
load "#{file}"
end
edit: complete example of Spork
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 all factories
FactoryGirl.factories.clear
Dir.glob("#{::Rails.root}/spec/factories/*.rb").each do |file|
load "#{file}"
end
# reload routes
YourAppName::Application.reload_routes!
end
I also encountered this error:
FactoryGirl::DuplicateDefinitionError: Factory already registered: factory_name
And I fixed the issue by adding :require => false in my Gemfile like this one:
gem 'factory_girl_rails', :require => false
Then require it in the test_helper.rb only once by having this line:
require 'factory_girl_rails'
Here is what worked for me in Rails 4.2.6
In rails_helper.rb add these librarys
require 'factory_girl'
require 'factory_girl_rails'
Add these to the config part
config.include FactoryGirl::Syntax::Methods
In my case, It happened when I accidentally put two factory files which contain same key as follows:
Rails.root/spec/factories/book.rb
Rails.root/spec/factories/books.rb
This is obvious, but this situation happened when I developed under no-git environment(!) and put source files by rsync(1). When I rename book.rb to books.rb from my local PC and rsync to dev-env, both book.rb and books.rb exist and this caused the situation here;-(
I'm trying to deploy a simple skeleton Rails 3.0.5 (and Ruby 1.9.2) app to Heroku, but encounter the same error when I open the website and when I try to migrate the db. I created the Heroku app with heroku create --stack bamboo-mri-1.9.2. Everything works locally.
Code: https://github.com/curiousyogurt/SEE
App: http://stormy-ice-778.heroku.com/
When going to the website, I get an "Application Error"; in the logs, I get the following error (followed by lots of other information):
2011-03-20T17:25:31-07:00 app[web.1]: /app/cfde9dd3-c394-45fb-a0ef-72a753e83909/home/.bundle/gems/ruby/1.9.1/gems/railties-3.0.5/lib/rails/railtie/configuration.rb:77:in 'method_missing': undefined method `action' for #<Rails::Application::Configuration:0x00000001d398d8> (NoMethodError)
Stack Trace: gist.github.com/878866
When doing heroku rake db:migrate, I get the following error:
rake aborted!
undefined method 'action' for #
/app/44666f97-ad08-444e-9f39-9ca7eb8fdc93/home/.bundle/gems/ruby/1.9.1/gems/railties-3.0.5/lib/rails/railtie/configuration.rb:77:in `method_missing'
Stack Trace: gist.github.com/878870
Here is my Gemfile (non-production parts removed):
gem 'rails', '3.0.5'
gem 'haml'
gem 'devise', :git => 'git://github.com/plataformatec/devise',
:branch => 'master'
gem 'omniauth'
I'm not sure where to go next in trying to track down this problem. Any suggestions would be greatly appreciated.
config.action.mailer.default_url_options = { :host => 'stormy-ice-778.heroku.com' }
in your config/environments/production.rb should be
config.action_mailer.default_url_options = { :host => 'stormy-ice-778.heroku.com' }
(change action.mailer to action_mailer).