Zeus + FactoryGirl::Syntax::Methods. undefined method `create' - ruby-on-rails

I have:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
which properly work with simple rspec spec/model/user_spec.rb (allows me to use create(:user), not FactoryGirl.create(:user)).
But if I use zeus rspec spec/model/user_spec.rb to speed up my specs, it troughs me an error:
Failure/Error: #user = create(:user)
NoMethodError:
undefined method `create' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fc8618e4960>
How I can use this syntax with zeus?

Remove either of these lines in spec/spec_helper.rb if they exist:
require 'rspec/autorun'
require 'rspec/autotest'

Did you previously use spork on this project? If so, you have to remove the parts that Spork changed in your spec_helper. Like #ilake-chang said, you have to remove the require 'rspec/autorun' and you'll also want to remove Spork.prefork and Spork.each_run.
See the Zeus wiki on Spork

Related

NoMethodError: undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class

Searching for this problem has only turned up issues with specific gems or classes that other people have dealt with. In my case, I think there's something wrong with rspec in general.
Whenever I generate a controler with rails generate ControllerName, it sets everything up and things seem to work fine. I've routed a few controllers and tested them in development and production and everything works.
The only thing that seems to be broken is testing them with rspec. Right now I have two controllers in my project, and whenever I run rspec/spec, most specs generated by rails g turns up this error:
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::MoreStuffHere
For example, I have a WelcomeController and ApplicationsController and these are the errors I keep getting:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeAboutHtmlErb:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeIndexHtmlErb:Class
Interestingly, I don't get errors for their helper_spec's
Here is a full error in case this helps:
An error occurred while loading ./spec/controllers/applications_controller_spec.rb.
Failure/Error:
RSpec.describe ApplicationsController, type: :controller do
end
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
# ./spec/controllers/applications_controller_spec.rb:3:in `<top (required)>'
Does anyone have any idea where this issue could lie?
In spec_helper.rb I had
require File.expand_path("../../config/environment", __FILE__)
require 'rails/all'
RSpec.configure do |config|
# More code here.
end
All I had to do was add require 'rspec/rails' under require 'rails/all'. This solved my problem.
Yet, I don't know why. If someone can elaborate that would be great. I already had require 'rspec/rails' in rails_herlper.rb, but obviously that wasn't good enough.
IMHO the solution here is somewhat what Damian Rivas says there, but the important thing is:
in rails helper you have a line:
require 'spec_helper'
in spec_helper you have in config block line to include devise helpers:
config.include Devise::Test::ControllerHelpers, type: :controller
you also as said above have to have rspec/rails require line
The main point there was that you have to watch the order here... always including rspec/rails has to go before spec_helper
So lets say example spec_helper.rb looks like:
require 'rubygems'
Rspec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
Then our rails_hepler.rb has to look like:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'spec_helper'
Then in sample_spec.rb you require rails_helper, or in my case I require through .rspec so I don't have to have it on top of every spec.
Hope it helps somebody :)

Use BestInPlace::TestHelpers with minitest

I'm trying to use this line in a minitest test that uses capybara, poltergeist, and phantomjs:
bip_select(#gs, :goal_id, Goal.first.name)
This is a helper that best_in_place offers to simulate a user choosing a value from a field. I've read a few questions elsewhere on StackOverflow where other developers who are using RSpec have added this line to their spec_helper.rb file:
config.include BestInPlace::TestHelpers
I've tried adding this line to my test_helper.rb file and I've tried adding it to the test in question. But I'm still getting the error
NoMethodError: undefined method `bip_select' for #<GoalStudentsPoltergeistEditTest:0x00000006d85148>
Thank you in advance for any insight.
To get the method definition available in your integration tests, edit test_helper.rb to include the lines referring to Best in Place (other lines left for context):
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'best_in_place/test_helpers'
# ...
class ActionDispatch::IntegrationTest
include BestInPlace::TestHelpers
end

How to add "config.include FactoryBot::Syntax::Methods" to rspec config block in spec_helper.rb?

If I add:
config.include FactoryBot::Syntax::Methods
under
RSpec.configure do |config|
and run rspec, I see this error:
/Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block
in ': uninitialized constant FactoryBot (NameError)
my gemfile.lock can be seen in this pastebin
my gemfile can be seen in this pastebin
If I omit the Rspec.configure statement, my tests all run fine. I'd like to make use of the abbreviated syntax, but am not sure what I am doing wrong here.
Note: FactoryBot was previously called FactoryGirl
Got it.
This link showed me the way.
The required addition should be made in spec/support/factory_bot.rb and it should look like this:
# RSpec
# spec/support/factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Note: FactoryBot was previously called FactoryGirl
You must add this string in file 'spec/RAILS_helper.rb' not in 'spec_helper.rb'
Also make sure to require 'factory_bot' in spec/support/factory_bot.rb
That's what ended up being the issue for me.
Make sure to include require 'support/factory_girl' in spec/rails_helper.rb after require 'rspec/rails'.
I was getting this error after putting it right after require 'spec_helper'.
This answer is compiled and tested from previous comments and factory_bot.
Create spec/support/factory_bot.rb file.
Paste inside spec/support/factory_bot.rb file:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Add require 'support/factory_bot.rb' in spec/rails_helper.rb file.
Write tests with pleasure.
I think it's worth mentioning here that, going forward, if using FactoryGirl, you will receive a deprecation message:
The factory_girl gem has been deprecated and has been replaced by factory_bot. Please switch to factory_bot as soon as possible.
Just a note for developers in the future that are trying to use FactoryGirl.

Capybara: undefined method 'visit'

When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?
$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)
Gemfile:
group :test, :development do
gem "rspec-rails"
gem "capybara"
end
top of my 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 'capybara/rspec'
require 'rspec/autorun'
homepage_spec.rb:
require 'spec_helper'
describe "The home page" do
context "home page exists" do
visit "/"
page.should have_content("elephants")
end
end
Just ran into this issue myself.
So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.
For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.
They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.
This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814
A few things to note here :
The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
load Capybara dsl explicitly inside your spec_helper
require 'capybara/rails'
require 'capybara/rspec'
include Capybara::DSL
This worked for me.
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara::DSL, :type => :request
end
This enables you to use Capybara's helpers inside spec/requests.
Because RSpec.configure not including capybara DSL in spec_helper.rb
It is an ugly solution, but you can add this to your spec_helper.rb.
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
The git issue for this:
https://github.com/rspec/rspec-rails/issues/503
Unfortunately this workaround doesn't do it for me. I still get
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>
The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'
edit: If i put include Capybara::DSL inside my describe block it works.
but including Capybara::DSL in the global scope is not recommended!
Because I do not know a good way.
For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".
Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.

"PGError: no connection to the server" on running specs with Spork

I'm using Ruby 1.9.2, Rails 3.1, Rspec, Postgres and Spork, but I can't get them to play nicely together.
Running the specs for the first time (with Spork running in the background) works fine. However, when I run the specs a second time, it fails with:
Failure/Error: Unable to find matching line from backtrace
PGError:
no connection to the server
# /Users/tom/.rvm/gems/ruby-1.9.2-p180#grapi/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/postgresql_adapter.rb:272:in `exec'
etc....
Any tips appreciated!
You probably also have Devise enabled.
Your problem is described here: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujutsu
And more specifically for rails 3.1 here: https://gist.github.com/1054078
The beginning of your prefork block in spec_helper.rb and env.rb should looks like:
Spork.prefork do
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
...
Good luck!
If you're using Factory Girl, don't use the 'factory_girl_rails' gem, just use 'factory_girl'.
Spork.each_run do
FactoryGirl.definition_file_paths = [
File.join(Rails.root, 'spec', 'factories')
]
FactoryGirl.find_definitions
end
For anyone using Factory Girl, Machinist, or Shoulda Matchers, make sure you read about Spork's trap_method at: https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
It solved my problems with Spork and the dropped PostgreSQL connections while testing.
you have to run spork --bootstrap
and after insert some configuration to your spec_helper.rb file, so spork knows about your rails configuration.
As you use RSpec, you can try adding the following code to your spec_helper file:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
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
# Needed for Spork
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end
Could you try adding this to Spork.each_run callback and check if it solves the problem?
ActiveRecord::Base.connection_pool.verify_active_connections!
I read the instructions on https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu and found the following.
In my case the solution was to change how machinist blueprints was loaded. My prefork block had this line:
Spork.prefork do
...
require Rails.root.join 'spec/support/blueprints'
...
I removed that from the prefork block and instead added this line to each_run:
Spork.each_run do
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
...
The two lines basically does the same thing, so the main thing seams to be not to load the blueprints in the prefork, but rather in each_run.
Hope it helps!

Resources