Ruby Gem: Uninitialized constant FactoryBot - ruby

Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
and in spec_helper.rb:
require 'support/factory_bot'
When I try to run the spec rake task, I get this error:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
What am I missing? This used to work fine when I was using the old factory_girl gem, but it has broken with the rename to factory_bot. Thanks!!

Doh. Silly mistake here, running bundle exec rake spec instead of rake spec solved it.
Also had to add require 'factory_bot' to the top of support/factory_bot.rb

Overview just in case you are doing this from scratch
installation rspec details here (basically add gem to Gemfile then run bundle install)
initialize RSPEC in your rails project rails g rspec:install
create new file your spec/support/factory_bot.rb add the following base code:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
add reference on spec/rails_helper.rb
require 'support/factory_bot'
as well as remove any fixture unused reference like this one
config.use_transactional_fixtures = true
That should be it!, finally run any spec file you want inside rspec default folders
e.g.:
spec/features/my_action_spec.rb
spec/models/my_model_spec.rb
spec/task/my_task_spec.rb
or run them all and check depending on your setup
rspec
rails rspec
bundle exec rspec
hope this helps someone with the whole RSPEC + FactoryBot Gem installation process

I had this problem too, remove the
require 'support/factory_bot'
There's a line on rails_helper, just uncomment it:
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

I had encountered and issue similar to this. I worked around it by removing the default testing suite ( MiniTest ). When you create a rails application and intend on using rspec and factory_bot, use the code below in the command line:
rails new myapp -T
Hope this helps xP

In my case I had to put those lines below 'require "rspec/rails" in file:
spec/rails_helper.rb:
like:
require "rspec/rails"
require_relative "support/factory_bot"
require_relative "support/chrome"

Related

Ruby gem dependency loading

I'm putting some shared models for a rails app inside it's own gem. It's just models, so I'm not using an engine. Getting it set up seemed to work fine until I added the "acts_as_list" gem.
# gem - domain.gemspec
spec.add_dependency "acts_as_list"
# gem - lib/domain.rb
require "acts_as_list"
# gem - lib/domain/models/page.rb
acts_as_list scope: [:ancestry]
This works fine in the console for my gem, I can run methods specific to acts_as_list as usual. However, when I add my gem to another project, it gives me an error.
# project - Gemfile
gem "www_domain", path: "../www_domain"
Bundler::GemRequireError: There was an error while trying to load the gem 'domain'.
NoMethodError: undefined method `acts_as_list' for #<Class:0x0055da70121ab0>
/home/shaun/sites/demo/domain/lib/domain/models/page.rb:32:in `<class:Page>'
Is there something special I have to do in this case to access the acts_as_list method because my model is inside a gem?
Update: Here is my complete lib/domain.rb file for the gem:
require "yaml"
require "active_record"
require "acts_as_list"
require "ancestry"
# rbfiles = File.join(File.dirname(__FILE__), "lib", "**", "*.rb")
# Dir.glob(rbfiles).each do |file|
# require file.gsub("lib/", "")
# end
module Domain
# Your code goes here...
def self.root
File.dirname(__dir__)
end
def self.db_config
YAML.load_file("#{Domain.root}/db/config.yml")["development"]
end
end
require "domain/version"
require "domain/models/page"
require "domain/models/menu"
require "domain/models/article"
require "domain/models/page_part"
I can use acts_as_list and ancestry methods in the console of my gem (running bin/console from the gem directory). But the project console (running bundle exec rails console from the project directory) will not start because of the gem error I mentioned.
This might result from a load order issue if the model being required before the require :acts_as_list statement. Check to see if the gem specifies the load order. If not, you could try something like:
# gem - lib/domain.rb
require "acts_as_list"
require "models/page"
If the load order is unclear, I find it helpful to simply add a
puts __FILE__
at the top of the relevant source files.

ruby/rake/rspec cannot load spec_helper (not rails)

I'm trying to set up a testing environment for a ruby project using Rake and Rspec.
When I try to run "rake" in the console I get this error:
C:/Ruby21-x64/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- spec_helper (LoadError)
from C:/Ruby21-x64/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from spec/numbersTest_spec.rb:1:in `<main>'
rake aborted!
My filetree looks like this:
project
-spec
--numbersTest_spec.rb
--spec_helper.rb
-rakefile
rakefile
begin
require 'rspec/core/rake_task'
task default: %w[test]
task :test do
ruby "spec/numbersTest_spec.rb"
end
end
numbersTest_spec.rb
require "spec_helper"
describe "Imperative" do
perfectImperative(5).should == false
end
require_relative 'spec_helper'
instead of require ... in numbersTest_spec.rb will solve this problem.
The reason is that numbersTest_spec.rb has no clue where to look the required file for. require expects an argument to be available on global require path. To solve this in general, one might update $: (global require path), by e.g.:
$:.unshift "#{`pwd`}".chomp
But in your particular case relative requiring is the silver bullet. In fact, bundler was invented to forget about loading path managing horror.

Using Rspec with Rails and Neo4j.rb: missing method

Please help,
I am doing a project with rails and neo4j with the neo4j.rb gem by ronge. I can get generator and CRUD working with Neo4j. But, everytime I run 'rspec' tests, there is method missing error within the configure block of RSpec within spec_helper.
Can anyone helps me figure this out?
Thanks so much!!
Rails and JRuby version.
saasbook#saasbook:~/temp$ rails -v
Rails 3.2.17
saasbook#saasbook:~/temp$ ruby -v
jruby 1.7.10 (1.9.3p392) 2014-01-09 c4ecd6b on Java HotSpot(TM) Client VM 1.7.0_51-b13 [linux-i386]
Create Rails App
rails new myapp -m http://andreasronge.github.com/neo4j/rails.rb -O -T
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.17'
gem 'jruby-openssl'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'therubyrhino'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :development, :test do
gem "rspec-rails"
end
gem "neo4j"
Then,
saasbook#saasbook:~/test/myapp$ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
saasbook#saasbook:~/test/myapp$ rails g model testnode
invoke neo4j
create app/models/testnode.rb
invoke rspec
create spec/models/testnode_spec.rb
When I run rspec, here is the error:
saasbook#saasbook:~/test/myapp$ rspec
NoMethodError: undefined method `fixture_path=' for #<RSpec::Core::Configuration:0xbcf6bf>
(root) at /home/saasbook/test/myapp/spec/spec_helper.rb:21
configure at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core.rb:120
(root) at /home/saasbook/test/myapp/spec/spec_helper.rb:11
require at org/jruby/RubyKernel.java:1083
(root) at /home/saasbook/test/myapp/spec/models/testnode_spec.rb:1
load at org/jruby/RubyKernel.java:1099
(root) at /home/saasbook/test/myapp/spec/models/testnode_spec.rb:1
each at org/jruby/RubyArray.java:1613
(root) at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:1
load_spec_files at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896
load_spec_files at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896
run at /home/saasbook/.rvm/gems/jruby-1.7.10/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22
Also, here is the generated spec_helper.rb file:
# 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 }
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"
end
Resolved, see comments on the questions.
:fixture_path and :use_transactional_fixtures are two settings added by rspec-rails ONLY under the presence of ActiveRecord. So of course they are method missing. Just comment them out, you don't need them if you use Neo4j.rb

How to use RSpec without Rails?

What is the process for doing TDD in Ruby with RSpec without Rails?
Do I need a Gemfile? Does it only need rspec in it?
Ruby 1.9.3
The process is as follows:
Install the rspec gem from the console:
gem install rspec
Then create a folder (we'll name it root) with the following content:
root/my_model.rb
root/spec/my_model_spec.rb
#my_model.rb
class MyModel
def the_truth
true
end
end
#spec/my_model_spec.rb
require_relative '../my_model'
describe MyModel do
it "should be true" do
MyModel.new.the_truth.should be_true
end
end
Then in the console run
rspec spec/my_model_spec.rb
voila!
From within your projects directory...
gem install rspec
rspec --init
then write specs in the spec dir created and run them via
rspec 'path to spec' # or just rspec to run them all
The workflows around gem install rspec are flawed. Always use Bundler and Gemfile to ensure consistency and avoid situations where a project works correctly on one computer but fails on another.
Create your Gemfile:
source 'https://rubygems.org/'
gem 'rspec'
Then execute:
gem install bundler
bundle install
bundle exec rspec --init
The above will create .rspec and spec/spec_helpers.rb for you.
Now create your example spec in spec/example_spec.rb:
describe 'ExampleSpec' do
it 'is true' do
expect(true).to be true
end
end
And run the specs:
% bundle exec rspec
.
Finished in 0.00325 seconds (files took 0.09777 seconds to load)
1 example, 0 failures

How to color unit tests with lib minitest or Test:Unit?

I would like to have unit tests output color in my dev environment. However, I can't make it work on Linux (Debian and Ubuntu). When I include the following libs:
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/pride'
I get:
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `<top (required)>': undefined method `output' for MiniTest::Unit:Class (NoMethodError)
caused by the code:
MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output)
I have seen a working Rspec variant. Unfortunately, my Ruby knowledge is not enough to see differences.
Give turn a whirl.
Add this to your Gemfile:
group :test do
gem 'turn', :require => false
end
step 1 : use the latest version of the gem (I think it will be fixed in Ruby 1.9.3)
gem install minitest
step 2 : require "minitest/pride" on the command line, not in your code
ruby -rminitest/pride your_ruby_script.rb
.. and in your code simply require 'minitest/autorun'
require 'minitest/autorun'
If you use Rubymine, just add
-rminitest
in the default configuration of the tests.
=> the configuration would like
-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -rminitest/pride
simply add these lines to your test_helper.rb file after require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
in your gemfile add
gem 'minitest-reporters', '~> 1.0.7'
this will get your rake test to be in red and green, but it also brings up the backtrace. to get rid of all those extra backtrace logs add this to your gemfile then bundle:
gem 'mini_backtrace'
then in config/initializers/backtrace_silencers.rb add this line to cut all the extra rvm stuff
Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }
hope that works for you, let me know if you need more details.
See https://github.com/tenderlove/purdytest/issues/1. It seems to be a known bug with the minitest version shipped with 1.9.2. For the others, adding
gem "minitest"
at the very top of your file did the trick.
I currently use purdytest with 1.9.2
Try look at this post about using turn gem for nice looking and configurable output for minitest.
http://rawonrails.blogspot.com/2012/01/better-minitest-output-with-turn-gem.html
$ gem install mynyml-redgreen --source http://gemcutter.org
# in your test file
require 'redgreen'
redgreen and turn work nicely in conjunction with each other, by the way

Resources