Hi people,
I’m having a little trouble getting spec to run with Factory Girl on my
new Rails 3 app.
/spec/spec_helper.rb:
This file is copied to spec/ when you run 'rails generate
rspec:install’
ENV[“RAILS_ENV”] ||= ‘test’
require File.expand_path("…/…/config/environment", FILE)
require ‘rspec/rails’
require ‘factory_girl’
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}
Dir[Rails.root.join("spec/factories/.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
end
/spec/factories/videos.rb:
Factory.define :video_one do |f|
f.title “MyString”
f.teaser “MyText”
f.link “MyString”
end
and /spec/controllers/video_controller_spec.rb:
require File.dirname(FILE) + ‘/…/spec_helper’
describe VideosController do
render_views
it “destroy action should destroy model and redirect to index action”
do
video = Factory(:video_one)
delete :destroy, :id => video
response.should redirect_to(videos_url)
Video.exists?(video.id).should be_false
end
end
results in:
VideosController destroy action should destroy model and redirect to
index action
Failure/Error: video = Factory(:video_one)
NameError:
uninitialized constant VideoOne
# ./spec/controllers/videos_controller_spec.rb:51:in `block (2
levels) in <top (required)>’
I’ve been googling etc. but not successful yet. Anyone know what’s wrong
there?
Did you create your VideoOne model yet? FactoryGirl requires that this
be present.
Here’s some general background on FactoryGirl
http://www.arailsdemo.com/posts/39.
I just tried the console and same error occurs:
ruby-1.9.2-p136 :016 > Factory.define :blah do |f| f.name “dasd”;
f.email “[email protected]” end
=> #<Factory:0x82d2ac8 @factory_name=:blah, @options={},
@attributes=[#<Factory::Attribute::Static:0x82ccb00 @name=:name,
@value=“dasd”>, #<Factory::Attribute::Static:0x82cc7cc @name=:email,
@value=“[email protected]”>]>
ruby-1.9.2-p136 :017 > Factory(:blah)
NameError: uninitialized constant Blah
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rspec-core-2.4.0/lib/rspec/core/backward_compatibility.rb:20:in
const_missing' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:355:in
const_get’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:355:in
block in class_for' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:354:in
each’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:354:in
inject' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:354:in
class_for’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:71:in
build_class' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:321:in
run’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:273:in
create' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl/factory.rb:304:in
default_strategy’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/factory_girl-1.3.3/lib/factory_girl.rb:20:in
Factory' from (irb):17 from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands/console.rb:44:in
start’
from
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands/console.rb:8:in
start' from /usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.0/lib/rails/commands.rb:23:in
<top (required)>’
from script/rails:6:in require' from script/rails:6:in
'ruby-1.9.2-p136 :018 >
…what’s wrong with this girl?
Yeah, just figured that out. Didn’t know FactoryGirl was so strict about
it.
That one worked:
Factory.define :video do |f|
f.title “MyString”
f.teaser “MyText”
f.link “MyString”
end
Thanks!