NameError: uninitialized constant Factory - ruby-on-rails

I'm following this tutorial to get started with TDD on rails with factory girl, rspec and i ran into this issue i can't get my head around.
Here's my "factory".rb (events.rb)
require 'faker'
FactoryGirl.define do
factory :event do
name "HIGH"
genre "house, techno, idb"
venue_name "Westbourne Studios"
venue_address "4-6 Chamberlayne Road"
venue_postcode "NW103JD"
begin_time "10pm"
end_time "2am"
user_id 2
description "A Super massive party with loads of everything you would want around."
status true
venue_id nil
end
end
and here's the event_spec.rb:
require 'spec_helper'
require 'factory_girl_rails'
describe Event do
it "has a valid factory" do
Factory.create(:event).should be_valid
end
it "is invalid without a name"
it "is invalid without a genre"
it "is invalid without a venue_name"
it "is invalid without a venue_address"
it "is invalid without a venue_postcode"
...
end
I have setup the model, migrated etc.. and when i run "rspec spec/models/event_spec.rb" i get the following error:
Failures:
1) Event has a valid factory
Failure/Error: Factory.create(:event).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/event_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.1682 seconds
13 examples, 1 failure, 12 pending
Failed examples:
rspec ./spec/models/event_spec.rb:6 # Event has a valid factory
Randomized with seed 64582

Try to use it in this way:
FactoryGirl.create(:event).should be_valid
I think, I can remember, that it was only "Factory" in old versions of the gem. If you take a look in the recent "Getting started" guide of Factory Girl, there are only calls with "FactoryGirl".

If you create file spec/support/factory_girl.rb with content:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Then you can simply use:
create(:event)
build(:book)
Instead of:
FactogyGirl.create(:event)
FactogyGirl.build(:book)

I had the same error with factory bot, and to supplement Stefan's answer I came across this little cheat sheet.
https://devhints.io/factory_bot

Related

rspec testing strong params and building a model

I am attempting to write a model test, like so:
require 'spec_helper'
describe Five9List do
before :each do
#five9_list = Five9List.new(name: 'test_list', size: '100')
end
describe "#new" do
it "takes two parameters and returns a Five9List object" do
#five9_list.should be_an_instance_of Five9List
end
end
describe "#name" do
it "returns the correct name" do
#five9_list.name.should eql "test_list"
end
end
describe "#size" do
it "returns the correct size" do
#five9_list.size.should eql 100
end
end
end
Currently, this succeeds and works fine. That's because my model is using attr_accessible, like so:
class Five9List < ActiveRecord::Base
attr_accessible :name, :size
end
If I want to get rid of attr_accessible and follow the rails 4 convention of using strong_params, how would I write that to where my rspec test would still succeed?
Adding this in my controller:
private
def five9_list_params
params.require(:five9_list).permit(:name, :size)
end
And removing attr_accessible does not work.
EDIT
Here is the error I receive from rspec .:
Failures:
1) Five9List#name returns the correct name
Failure/Error: #five9_list.name.should eql "test_list"
expected: "test_list"
got: nil
(compared using eql?)
# ./spec/models/five9_list_spec.rb:16:in `block (3 levels) in <top (required)>'
2) Five9List#size returns the correct size
Failure/Error: #five9_list.size.should eql 100
expected: 100
got: nil
(compared using eql?)
# ./spec/models/five9_list_spec.rb:22:in `block (3 levels) in <top (required)>'
Finished in 0.03303 seconds
4 examples, 2 failures, 1 pending
Failed examples:
rspec ./spec/models/five9_list_spec.rb:15 # Five9List#name returns the correct name
rspec ./spec/models/five9_list_spec.rb:21 # Five9List#size returns the correct size
Randomized with seed 20608
There's nothing wrong with your spec. I can only guess that you're not running Rails 4 or you've installed the ProtectedAttributes gem.

undefined method `first_name=' Rspec + Factories

I've been building my application and I'm now ready to start testing. I have Factory girl defined in seeds.rb but as I'm running tests I've also defined the tests in the usual place /spec/factories.rb for Rspec.
However my first tests fails with the following error.
user_spec.rb
require 'spec_helper'
describe User do
it "should have valid factory" do
FactoryGirl.build(:user1).should be_valid
end
end
Error returned:
Failures:
1) User should have valid factory
Failure/Error: FactoryGirl.build(:user1).should be_valid
NoMethodError:
undefined method `first_name=' for #<User:0x000000061410f8>
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.05459 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:4 # User should have valid factory
Randomized with seed 29084
spec/factories.rb
FactoryGirl.define do
factory :admin1, class: User do
first_name "admin"
last_name "minstrator"
password "admin1234"
profile_name "profilename"
email "admin#admin.com"
password_confirmation "admin1234"
admin true
end
factory :user1, class: User do
first_name "user2"
last_name "man2"
password "user1234"
profile_name "profilename"
email "user2#user.com"
password_confirmation "user1234"
admin false
end
end
It works fine when using the data on development in my seed.rb but now I've started testing using Rspec it has all gone wrong.
What am I doing incorrectly here. I'm not a huge fan of testing at the moment but I need to improve my skill set here as I know it can be extremely useful for web applications.
You help is greatly appreciated, please let me know if you need anymore info.
If your code works in development, but not in test, my guess is that your test copy of the database is not in sync. Try running rake db:test:prepare or rake db:test:clone and run your specs again.
Note:
db:test:clone isn't required in Rails 4.2.0 'WARNING: db:test:clone is deprecated. The Rails test helper now maintains your test schema automatically, see the release notes for details.')
I believe the issue here was that one of my migrations was missing and because it didn't have users first_name in the model there was nothing defined.
A big tip to all newbies, never ever ever delete any of your migrations.
As Homer Simpson quite rightly said "D'oh"

FactoryGirls randomly fails with 'Factory not registered', why?

I've some tests that randomly fail, approx. 20% of times. It means that WITHOUT changing the code, each time that I run the tests 1 time out of 5 will fail with "Factory not registered" error. It's very weird.. :(
This is the consone output:
Failures:
1) Unit#new_from_string returns factor for metric conversions
Failure/Error: FactoryGirl.create :taza
ArgumentError:
Factory not registered: taza
# ./spec/models/unit_spec.rb:29:in `block (2 levels) in <top (required)>'
Finished in 0.29619 seconds
4 examples, 1 failure
Failed examples:
rspec ./spec/models/unit_spec.rb:22 # Unit#new_from_string returns factor for metric conversions
Randomized with seed 61727
And this is the code:
file: "unit_spec.rb"
require 'spec_helper'
describe Unit, "#new_from_string" do
it "parses the string and returns a Unit object" do
[some tests...]
FactoryGirl.find_definitions
u = FactoryGirl.create :taza
FactoryGirl.create :tbsp
[tests...]
end
it "returns factor for metric conversions" do
[tests not involving factory girl...]
# the following is line 29, that fails
FactoryGirl.create :taza
[tests...]
end
end
file "spec/factories/units.rb":
FactoryGirl.define do
factory :taza , :class => CustomUnit do
singular 'taza'
plural 'tazas'
physical_type Unit::VOLUME
equivalence_factor 200
equivalence_unit 'ml'
end
[other factories...]
end
I think the problem is on this line
FactoryGirl.find_definitions
Actually there is no need for this line when your factories are in correct directories(I see it is), and you put gem factory_girl_rails in Gemfile.
I think, 20% of time, the second test get run at first. At this time, there is no definition of Factory and the test failed. The other test has such definition and get passed. In other time, the first test run first so definition exists.
My suggestion:
Make sure you have factory_girl_rails, not factory_girl in Gemfile.
Remove that line of definition.
[optional but recommended] Put all definition in a single file spec/factories and remove all other factory files, if you don't have too much factories. This would be easier to manage.

Rails Engine with RSpec and FactoryGirl

I followed a few tutorials and docs of FactoryGirl to use with RSpec. Currently I get one error when trying to use FactoryGirl.create:
describe "GenericRecipesController" do
describe "GET 'index'" do
it "displays list of generic recipes" do
generic_recipe = FactoryGirl.create(:generic_recipe)
visit '/recipe'
response.should be_success
end
end
end
And the error:
GenericRecipesController GET 'index' displays list of generic recipes
Failure/Error: generic_recipe = FactoryGirl.create(:generic_recipe)
NameError:
uninitialized constant GenericRecipe
# ./spec/integration/generic_recipes_spec.rb:8:in `block (3 levels) in <top (required)>'
The rest of code is there.
You can try this:
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
# ...
end
I think problem in a nesting model in module
Upd: delete file /spec/factories.rb, in file /spec/support/factories.rb make
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
When you will run tests, probably will see 'can not load table'. Make
rake db:migrate RAILS_ENV=test
and try again.
You don't seem to have a GenericRecipe model in your app. Factory Girl is looking for a Model called GenericReciper and can't find it.

Shoulda for Rspec, Rails, outputs there is an error but doesn't output the error itself

Well, I'm doing some testing right with Rails+Rspec+Shoulda.
When I do a test like the following:
context #user do
describe 'Validation' do
describe :name
it { should allow_value('something').for :name }
end
end
end
When it fails, Rspec just output:
1) Validation name Valid
Failure/Error: it { should allow_value(value).for :name }
Did not expect errors when name is set to "something", got error:
# ./spec/models/user_spec.rb:4:in `block (3 levels) in <top (required)>'
It even says got error: but it doesn't output it! I actually know there is a validation error there, but I want Rspec to tell me that, how I would know what is failing to validate then?
What am I doing wrong? Is that the expected behaviour? I have to overwrite the helpers?
I dug into the Shoulda code and I found that it doesn't show the errors when checking for positive assert. But them are loaded into the #errors variable. So I just monkey patched the one method that defines the output:
module Shoulda
module ActiveRecord
module Matchers
def failure_message
"Did not expect #{expectation}, got error: \n#{#expected_message ? #matched_error : #errors.join("\n ")}"
end
end
end
end
The original said:
"Did not expect #{expectation}, got error: #{#matched_error}"
I saved it to /lib/shoulda/activerecord/matchers.rb and loaded it with config.autoload_paths += Dir["#{config.root}/lib/**/"]
Hope this helps someone with the same issue ^^
Yup welcome to spec testing so you need to recreate the error in console if you want the error, rspec is not a debugger just a test suite.
I run into this a lot

Resources