Is there a difference between factory_girl and factory_girl_rails Ruby Gems? I have a recurring problem with an error in RSpec Testing: "uninitialized constant FactoryGirl (NameError)".
Somebody told me that there is a difference between the two (this is really confusing) and one needs the other to work or something along those lines?
My spec_helper file has both:
require 'factory_girl'
require 'factory_girl_rails'
My Gemfile has:
gem 'factory_girl_rails'
Here is the full error:
uninitialized constant FactoryGirl (NameError)
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
from /srv/homes/rvm/gems/ruby-2.0.0-p247#global/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'
****#epi-stu-hut-shell3:~/projects/project4/spec/factories$
You only need:
group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails"
end
because factory_girl_rails automatically incorporates the factory_girl gem and adds support for Rails.
These gems go in both test and development groups because Rails generators will create stub files in development and of course they are needed in the test environment.
There's no need to add factory_girl or factory_girl_rails to your specs/spec_helper.rb file.
Add factory_girl_rails to your Gemfile under test group
group :test, :development do
gem "factory_girl_rails"
end
then do NOT do a require in spec_helper, it should get loaded automatically during tests; use bundle exec if needed when running rspec
Related
I'm following along, "Ruby on Rails Tutorial 3rd Edition" by Michael Hartl. I'm in Chapter 3, on the very first attempt to use the test command.
There are two extremely basic pages - home and help - with only text on them. I think the following tests are supposed to check that the pages exist:
sample_app/test/controllers/static-pages.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
However, when I run the command
$ bundle exec test rake
in the terminal window, I receive a long error that begins with
rake aborted!
NoMethodError: undefined method `web_console' for ActiveRecord::Base:Class
I've read a variety of suggestions that seem to revolve around gem files, in particular the one for 'web-console.' I've tried moving the gem around a bit to no avail but I'm willing to try it all again. Here is the current gem file:
sample_app/Gemfile (comments removed)
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'web-console', group: :development
group :development do
gem 'sqlite3'
gem 'byebug'
gem 'spring'
gem 'guard'
gem 'guard-minitest'
end
group :test do
gem 'minitest-reporters'
gem 'mini_backtrace'
end
Here is the entire error code if you're interested:
Running via Spring preloader in process 6353
rake aborted!
NoMethodError: undefined method `web_console' for ActiveRecord::Base:Class
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.2/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:16:in `<class:StaticPagesControllerTest>'
/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:5:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.2/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
I may have found an answer: Log off Cloud9. Turn off computer. Restart computer. Log back into Cloud9. Type "bundle exec rake test." And it's fixed. Magic!
On a more serious note, I realize I'm using an older version of the tutorial but the tutorial does direct the student to set up the environment in a way to fit this edition (I believe). Nevertheless, I did see a warning by Cloud9 at some point that the version of Ruby (2.3.0) I was using was "buggy" and to upgrade it to 2.3.1. I don't know if the buggy-ness of Ruby 2.3.0 was the culprit or perhaps buggy-ness in Cloud9, but it's something to be aware of for anyone else using an older version of the Tutorial.
I am newbie in rspec test.
I have a file /spec/models/class_spec.rb. The content of the file is
require 'rails_helper'
RSpec.describe Class, :type => :model do
#pending "add some examples to (or delete) #{__FILE__}"
before(:each) do
#my_class=Factory.create(:class, params)
end
end
I also have a /spec/rails_helper file. When I run rspec in command line
command : rspec spec/models/class_spec.rb
I gets the following error
/usr/local/rvm/gems/ruby-2.3.1/gems/airbrake-ruby-1.4.4/lib/airbrake-ruby.rb:102:in `configure': the 'default' notifier was already configured (Airbrake::Error)
from /home/akhil/test-prjct/ver3/this_prjct/config/initializers/errbit.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
Can someone tell me about issue and also give a link to a tutorial for learning to write test codes using rspec. Thanks in advance
My gem list is
group :development, :test do
gem "rails-erd"
gem 'letter_opener'
gem 'spring'
gem 'faker'
gem 'pry-rails'
gem 'pry-byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
end
errbit.rb file has following content
Airbrake.configure do |config|
config.host = 'https://192.168.2.143:3000'
config.project_id = -1
config.project_key = '76068df1155420d2658i35a75o95gk26'
config.environment = Rails.env
config.ignore_environments = %w(development test)
end
I'm following the NetTuts intro to Rails screencast to get a better handle on rspec/capybara/guard etc. and after a few errors I cannot get passed this one:
/home/jonlee/.rvm/gems/ruby-2.1.1#railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:15:in `<top (required)>': undefined method `join' for nil:NilClass (NoMethodError)
from /home/jonlee/Projects/rails/guardtest/spec/spec_helper.rb:3:in `require'
from /home/jonlee/Projects/rails/guardtest/spec/spec_helper.rb:3:in `<top (required)>'
My spec_helper is as follows:
require 'rails'
require 'rspec/core'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL
end
my gemfile has:
group :test, :development do
gem 'rspec-core'
gem 'capybara'
gem 'guard-rspec'
end
im using:
Ruby - 2.1.1
Rails - 4.0.5
rspec - 3.0.1
capybara - 2.3.0
Even after suggestion of changing spec_helper.rb file to require 'rspec/core' and changing gem to rspec-core I still have this error.
Does the order in the spec_helper matter or do I need to perform some further work in the Rspec.configure block?
GIT - https://github.com/JonleePeakman/guardtest
Your gemfile has gem 'rspec-core'. It should be gem 'rspec-rails'.
You have RSpec 3.0.1 and there are big changes in configuration compared to previous versions. Be careful not to follow outdated tutorials or blog posts. Have you used the RSpec generator to set up RSpec?
$ rails generate rspec:install
You should have files:
.rspec
spec/spec_helper.rb
spec/rails_helper.rb
Capybara will work "out of the box" without any changes to the configuration files. Try removing your spec/spec_helper.rb file and use the RSpec generator to set things up.
As an alternative to the NetTuts tutorial, you might want to look at the RSpec Tutorial I've written which is up to date with RSpec 3.0.
I am newbie for ruby on rails i am just reading the http://ruby.railstutorial.org/chapters/static-pages#top as the tutorial saying, i have created the sample_app bundle or project. Now in section 3.2.1 i am testing the app.
first i run the following from the cmd
$ rails generate integration_test static_pages
This created the static_pages_spec.rb in the spec/requests directory.
Now add the following code in spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
at this stage i add the following code in sample_app/spec/spec_helper.rb file
config.include Capybara::DSL
so my file look like
# 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 '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 }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
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
# 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
# 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 Capybara::DSL
end
i have app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
and run the command
$ bundle exec rspec spec/requests/static_pages_spec.rb
it was giving the error
c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:42:in `block in <top (
required)>': uninitialized constant Capybara (NameError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core.rb:120:in `configure'
from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:15:in `<t
op (required)>'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/command_line.rb:22:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:80:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:17:in `block in autorun'
i have installed the capybara as per direction in git
step 1->gem install capybara
step 2->require 'capybara/rails' in sample_app/spec/spec_helper.rb
but still it was showing the load error of capybara rails
now i run the same command i.e $ bundle exec rspec spec/requests/static_pages_spec.rb
i got the following error
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.0/lib/act
ive_support/dependencies.rb:228:in `require': cannot load such file -- capybara/
rspec (LoadError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:228:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:8:in `<to
p (required)>'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/command_line.rb:22:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:80:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:17:in `block in autorun'
and my gemfile is
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
group :development, :test do
gem 'rspec-rails', ">= 2.0.0.beta"
end
gem 'rubyzip'
if i remove the config.include Capybara::DSL then i get fail error
Please give me right direction ...thanks in advance
Since you're using Bundler (i.e. you have a Gemfile), you need to make sure Capybara is in there too, otherwise your rails app won't know the gem is installed. Update your Gemfile to include capybara in the development/test environments:
group :development, :test do
gem 'rspec-rails', ">= 2.0.0.beta"
gem 'capybara'
end
When running my rails 4 rspec suite via rake everything works correctly but when attempting to run rake simplecov I get hundreds of failures all w/ a NoMethodError something like:
1) Answer Validations
Failure/Error: it { should validate_presence_of :text }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_16::Nested_1:0x007faf9fb5b7c8>
# ./spec/models/answer_spec.rb:12:in `block (3 levels) in <top (required)>'
Any clues as to why this is happening? I should also mention that I'm testing using sqlite3 :memory:.
The shoulda libraries aren't being included in your simplecov run.
Is simplecov run in a different environment than :test perhaps? That may make the shoulda gem not be loaded if it's only in the :test group in your Gemfile.
How are you including simplecov in your spec_helper? Something like this?
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails' if ENV['COVERAGE']
As the very first lines of your spec_helper.rb?
Something like this in your Gemfile?
group :test, :development do
...
gem 'rspec-rails', '~> 2.0'
gem 'shoulda-matchers'
end
group :test do
gem 'simplecov'
gem 'simplecov-rcov'
end
And executing it like so?
$ COVERAGE=true bundle exec rake spec
This recipe is working very well for me in many projects. The RCov formatter may not be important depending on your purposes.