RSpec can't find Factorys from Factorygirl - ruby-on-rails

i will use RSpec with Factory girl in my Rails3 Project. I have installed factory girl but it don't find the factorys i have this error
Failure/Error: Factory.build(:user).should_be valid
No such factory: user
spec/factories/user_factory.rb :
Factory.define :user do |u|
u.username 'otto'
end
spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
end
Gemfile:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
end
Thanks

Do you have the following lines in your config\application.rb:
# Configure generators values.
config.generators do |g|
g.test_framework :rspec, :fixture => true
g.fixture_replacement :factory_girl, :dir=>"spec/factories"
end

Add the 'factory_girl_rails" gem to your Gemfile under your :test, :development groups, as follows:
group :development, :test do
gem 'webrat'
gem "cucumber-rails"
gem "rspec-rails"
gem "rspec"
gem "autotest"
gem 'factory_girl'
gem 'factory_girl_rails'
end
In Rails 3, you need to add that gem to make it work. Hope it helps.

have you tried adding something like
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
in the spec_helper?
that did it for me

If your rspec config is loading everything under spec/support then you can put your factories dir there.
See this post: http://www.codeography.com/2010/04/30/using-factory_girl-and-rspec-with-rails-3.html

Related

Rspec undefined method `expects' for Object

I have recently upgraded my ruby on rails project to rails 5.0.7 and ruby 2.5.1 and I am getting an RSPEC error undefined methodexpects' for` different objects I am testing.
I have tried adding a configuration in the spec_helper.rb file as suggested here (although I did a quick search and didn't find :should defined anywhere), here and here:
config.expect_with :rspec do |expectations|
expectations.syntax = [:expect, :expects]
end
Also tried including include RSpec::Matchers in my spec_helper.rb, but then I get even more errors:
`only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect(...).to`, but you have provided: #<RSpec::Matchers::BuiltIn::Eq:0x00007f962b3db058>`
An example of how I am using expect:
describe "process" do
before :each do
#item = Item.new
end
it "parses the uploaded file and extract the correct report data" do
item_a_data = {"name" => "one"}
item_b_data = {"name" => "two"}
file_contents = {"items" => [item_a_data, item_b_data]}
#item.data_file = double("file", :read => file_contents.to_json)
#item.name = item_a_data["name"]
#item.expects(:interpret_json_file).with(#item.data_file).returns(file_contents)
#item.expects(:save).returns(true)
expect(#item.process_data_file).to be_truthy
expect(#item.data).to eq item_a_data.to_json
end
Notice that the error occurs when I call #item.expects
In my Gemfile I have the following gems (among others):
group :development, :test do
gem 'byebug'
gem 'sqlite3'
gem 'rspec-rails'
gem 'rb-fsevent', '~> 0.9.1'
gem 'guard-rspec', '4.7.3'
gem 'guard-spork', '2.1.0'
gem 'spork', '0.9.2'
gem 'jasmine-rails'
gem 'teaspoon-jasmine'
end
group :test do
gem 'capybara'
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
gem 'factory_girl_rails', '4.1.0'
gem 'launchy'
gem 'poltergeist'
gem 'timecop'
gem 'webmock'
gem 'simplecov', '~> 0.16.0'
gem 'rails-controller-testing'
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# 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 'devise'
require './spec/controllers/controller_helpers.rb'
# 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|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Capybara::DSL
# 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
config.infer_spec_type_from_file_location!
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include FactoryGirl::Syntax::Methods
# config.expect_with :rspec do |expectations|
# expectations.syntax = :expect
# end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
The solution would be to re-write it to the following syntax:
expect(#item).to receive(:interpret_json_file).with(#item.data_file) { file_contents }
expect(#item).to receive(:save) { true }
Basically ActiveRecord model does not know about rspec expectations and it should not. If that specs were working before then you might used some decoration for ActiveRecord models and that is why it worked previously.

Factory not registered: user (ArgumentError)

I am using rspec-rails, factory_girl_rails and mogoid_rspec gems. After adding factory girl gem, I keep getting the error Factory not registered: user (ArgumentError) for my user factory. Following are the related code snippets:
In my Gemfile:
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem 'database_cleaner'
gem 'faker'
gem 'shoulda-matchers'
gem 'factory_girl_rails'
end
rails_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__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factory_girl_rails'
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions
require 'support/mongoid'
require 'support/factory_girl'
require 'support/disable_active_record_fixtures'
require 'mongoid-rspec'
ENV["RAILS_ENV"] ||= 'test'
RSpec.configure do |config|
config.before(:all) do
FactoryGirl.reload
end
config.include Mongoid::Matchers, type: :model
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
end
factories/user.rb
FactoryGirl.define do
factory :user do
first_name Faker::Name.first_name
last_name Faker::Name.last_name
email Faker::Internet.email
end
end
spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec_helper.rb
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Any help would be much appreciated. Thanks in advance! :)
As we've discussed, I'm pasting my proposition of what you could change in your code (including good practices).
Define factory_girl_rails gem inside group :test, :development.
#Gemfile
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem 'database_cleaner'
gem 'shoulda-matchers'
end
Require your support files in one single line instead of multiply
definitions.
# spec/rails_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.before(:all) do
FactoryGirl.reload
end
config.include Mongoid::Matchers, type: :model
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
config.use_transactional_fixtures = false
config.infer_spec_type_from_file_location!
end
Create your .rspec file (or edit yours if you already have it) to
require spec_helper and rails_helper without having to call it manually in every single spec.
I'd also suggest for everyone to
color and format the spec output.
# .rspec
--color
--format documentation
--require spec_helper
--require rails_helper

Factory Not Registered in rspec but found in console

I have the followed
group :test, :development do
gem 'rspec-rails', '~> 3.0'
gem "guard-rspec"
gem 'capybara'
gem 'factory_girl_rails'
gem 'turn'
gem 'guard' # NOTE: this is necessary in newer versions
gem 'poltergeist'
gem 'phantomjs', :require => 'phantomjs/poltergeist'
gem 'selenium-webdriver'
gem 'capybara-screenshot'
gem 'capybara-webkit'
gem 'zeus'
gem "parallel_tests"
gem 'launchy'
gem 'email_spec'
gem 'action_mailer_cache_delivery'
gem 'protractor-rails'
gem 'database_cleaner'
and in my spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'support/request_helpers'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include RequestHelpers
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Finally I have such a spec helper in the spec/support/request_helper.rb
module RequestHelpers
def create_logged_in_user
user = create(:user)
login(user)
user
end
def login(user)
login_as user, scope: :user
end
end
and factories.rb
# /spec/factories/factories.rb
FactoryGirl.define do
factory :user do
first "John"
last 'Smith'
email 'test#example.com'
password 'johnsmith123'
end
end
Yes everytime I run rpsec it shows that
Factory not registered: user
And when I rune FactoryGirl.factories in the rails console, i can see 'user' is registered
=> #<FactoryGirl::Registry:0x007fa4e0e18160
#items=
{
:user=>
#<FactoryGirl::Factory:0x007fa4e2371200
#aliases=[],
#class_name=nil,
#compiled=false,
#definition=
#<FactoryGirl::Definition:0x007fa4e2370f58
#additional_traits=[],
#attributes=nil,
#base_traits=[],
#callbacks=[],
#compiled=false,
#constructor=nil,
#declarations=
#<FactoryGirl::DeclarationList:0x007fa4e2370ee0
#declarations=
[#<FactoryGirl::Declaration::Static:0x007fa4e2370b48 #ignored=false, #name=:first, #value="john">,
#<FactoryGirl::Declaration::Static:0x007fa4e2370a80 #ignored=false, #name=:last, #value="smith">,
#<FactoryGirl::Declaration::Static:0x007fa4e23709b8 #ignored=false, #name=:email, #value="test#example.com">,
I read through the Github setup docs for factory_girl_rails, rspec-rails multiple times but found no solution.
Does anyone know where I should look to detect the problem?
Thanks!
EDITED added contents of spec/rails_helper.rb updated Wed, 10 Jun 2015
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/rspec'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :phantomjs => Phantomjs.path, :inspector => true)
end
Capybara.javascript_driver = :webkit
include Warden::Test::Helpers
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
end
I had the same issue
Why is FactoryBot not finding our factories only inside RSpec, I do not know.
But making FactoryBot explicitly find my definitions worked.
So you just need to add this to your spec_helper:
FactoryBot.find_definitions
in my case, I followed the general instructions in this answer:
https://stackoverflow.com/a/49436531/8790125
So I create a file spec/support/factory_bot.rb
with this code (note the find_definitions):
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
FactoryBot.find_definitions
end
and then required on rails_helper:
# ...
require 'rspec/rails'
require 'support/factory_bot'
# ...

`alias_method': undefined method `to_json' for class `Object' (NameError)

I want running my test through Rspec but getting errors full trace here
one of them is 'alias_method': undefined methodto_json' for class `Object' (NameError)' and i even dont know from where it comes
require 'rubygems'
require 'spork'
Spork.prefork do
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'capybara/rspec'
require 'rspec/rails'
require 'database_cleaner'
require 'factory_girl'
Dir[Rails.root + 'spec/support/**/*.rb'].map &method(:require)
RSpec.configure do |config|
config.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.mock_with :rspec
config.order = :random
config.use_transactional_fixtures = false
config.use_instantiated_fixtures = true
config.global_fixtures = :all
config.infer_spec_type_from_file_location!
config.raise_errors_for_deprecations!
config.output_stream = 'rspec.xml'
config.formatter = RspecJunitFormatter
config.before do
DatabaseCleaner.start
ActionMailer::Base.deliveries.clear
end
config.after do
DatabaseCleaner.clean
end
end
end
chunk of gemfile
group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0'
gem 'faker', git: 'https://github.com/stympy/faker.git'
gem 'pry'
gem 'rspec-rails', '~>3.0.0'
gem 'selenium-webdriver'
gem 'spork', :github => 'sporkrb/spork'
gem 'spork-rails', :github => 'sporkrb/spork-rails'
gem 'rspec_junit_formatter', :git => 'https://github.com/sj26/rspec_junit_formatter.git'
gem 'rspec-core'
gem 'rubocop', require: false
end
Any suggestions how to fix it?
Ruby 2.2.0
Rails 4.1.9

Configure Rspec with Mongoid

I am running Rails 3 with Mongoid 3.
I am trying to remove active record and replace RSpec test with Mongoid
This is my config/test.
this is my spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.before(:each) do
DatabaseCleaner[:mongoid].start
end
config.after(:each) do
DatabaseCleaner[:mongoid].clean
end
end
This is my Gemfile (for dev and test)
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', ">= 2.0.0.beta"
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
end
group :test do
gem 'capybara', '1.1.2'
gem "database_cleaner", "~> 1.1.1"
gem 'factory_girl_rails', '4.1.0'
gem "rb-inotify", "~> 0.9.0"
gem "libnotify", "~> 0.8.0"
end
However when a simple array splitter test(no db action)
require 'spec_helper'
describe ArrayHelper do
describe "split array" do
it "splits array up to three" do
testArray = [1,2]
result = splitTasks(testArray)
result.should eq(Array(Array(1),Array(2)))
end
I get the error
`method_missing': undefined method `active_record' for #<Rails::Application::Configuration
Ideas?
end
end
I think you should comment out
require "active_record/railtie"
From within confing/application.rb file.
I hope this helps :)

Resources