I cant run my rspec in Ruby on Rails - ruby-on-rails

I cant really get going with my Rspec-gem :) Unfortunately. It seems like the Rspec-rails gem works differently now? Any idea?
This is my error Im getting:
testing_rspec/spec/models/transport_spec.rb:2:in `<top (required)>': uninitialized constant Transport (NameError)
from /Users/yoniPacheko/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load'
My spec file:
require 'spec_helper'
describe Transport do
end
This is my file
Thanks for yr time!

If this is a newly-generated app, you should be using require 'rails_helper', instead of requiring spec_helper.
rspec-rails 3 has split the two out so that spec_helper can be used for standalone Ruby code, and rails_helper (which includes spec_helper) can be used for testing code that relies on Rails.

Related

How do I solve this rspec-support version error?

When I try running the rspec file of a task of my ruby application by using the command
rspec /Users/priyanshu.sahoo/Desktop/Fk/ltl-worker/spec/tasks/four_kites_common/background_job/tasks/calculate_auto_delivery_appointment_time_task_spec.rb
I get this error.
An error occurred while loading spec_helper. - Did you mean?
rspec ./spec/spec_helper.rb Failure/Error: require 'simplecov' LoadError: cannot load such file -- simplecov
#./spec/spec_helper.rb:1:in `<top (required)>' No examples found.
This is my first time working with a rails application. Can anyone help me resolve this?
I am using ruby 2.7.5p203, rails 4.2.11.1

Guard + Zeus + Rspec-Rails: undefined method 'configure' for Rspec:Module

I'm using the following:
Rails 4.1.1
guard-zeus 2.0.0
rspec-rails 3.0.1
Out of box default rails g rspec:install and guard init
When I run guard and save a spec file, I get the error:
undefined method `configure` for RSpec:Module (NoMethodError)
I can run specs with rspec spec and rake just fine.
In spec_helper, if I require 'rspec/rails before the configure block,
guard works fine, but then rspec spec fails with the error:
uninitialized constant ActiveSupport::Autoload (NameError)
I'm guessing there's a problem with load order now that rails_helper and spec_helper
are separated.
Two questions:
How can I fix this?
Is there a different solution for continuous integration locally that you can recommend that works with latest Rails and Rspec.
You only have to answer one question.
The following fix worked for me:
#spec/spec_helper.rb
require 'rspec/core'
Throwing out a quick answer that may be the problem. Your spec_helper file should have the following order:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
rspec/rails needs to be required after the config/environment require.
The following:
undefined method `configure` for RSpec:Module (NoMethodError)
suggests you are are missing a
require 'rspec'
This normally isn't necessary, but if you put it in your spec/spec_helper.rb that should work.
(If you run RSpec directly, it's included already with RSpec).
The reason it's not included is perhaps:
you are not running guard through bundler
or your Gemfile does not have:
gem 'rspec' # without the require: false
or something may be wrong with your .rspec file (which should be present)
The require 'rspec/rails' should probably go into the spec/rails_helper.rb...
... but a better way would be to update your rspec-rails gem and run:
rails generate rspec:install
and if you're prompted - used 'd' to differences (and ideally use the recommended changes).
You should add following require to top of file spec_helper.rb
require 'rspec/rails'
Take the reference here: Zeus GitHub issue 308

Uninitialized constant ApplicationHelper::Twitter

What's up guys?
I'm using the Twitter-Text gem in my RoR project, but I'm having troubles with the ApplicationHelper. I already see another topics with this same question on StackOverflow, but no one fix my problem.
Ok, let's take a look at my project:
Gemfile
gem 'twitter-text'
Application_Helper.rb
module ApplicationHelper
require 'twitter-text'
include Twitter::Autolink
include Twitter::Extractor
end
// I really don't know why I need create the following file:
helpers/twitter.rb
module TwitterText
end
helpers/twitter_helper.rb
module TwitterText
end
controller/posts_controller.rb
require 'twitter-text'
include Twitter::Extractor
What I see in localhost error page:
Routing Error
uninitialized constant ApplicationHelper::Twitter
Try running rake routes for more information on available routes.
What I see in my console:
ActionController::RoutingError (uninitialized constant ApplicationHelper::Twitter):
app/helpers/application_helper.rb:3:in `<module:ApplicationHelper>'
app/helpers/application_helper.rb:1:in `<top (required)>'
app/controllers/application_controller.rb:1:in `<top (required)>'
It's appear to be everything ok, what can I do to fix this trouble?
Thanks a lot!
whenever installing new gems you have to restart your web server as your gems are loaded when the application environment starts. Those are installed to your global gem directory by doing a bundle install.

"uninitialized constant" when running RSpec tests with Rails 2.3.8 and Bundler

I just changed my Rails 2.3.8 project to load gems using Bundler, rather than the default Rails 2.3 loading mechanism. I followed the official instructions and the site runs fine in development. I use RVM for gem management, and have a specific gemset loaded for the application.
My RSpec test suite is unable to run, however. I have tried running both the entire suite and single tests using a variety of commands:
autospec # My usual way of running tests
rake spec
spec .
spec spec/models/comment_spec.rb # Trying a single spec file in isolation
ruby spec/models/comment_spec.rb
I have also tried running them all prefixed with bundle exec. Everything returns the same error:
/Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant InheritedResources::Base (NameError)
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:80:in `const_missing_not_from_s3_library'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing'
from /Users/casper/Projects/cf/darebusters/app/controllers/admin/base_controller.rb:1
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:158:in `require'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:158:in `require'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:265:in `require_or_load'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:224:in `depend_on'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:136:in `require_dependency'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:414:in `load_application_classes'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:413:in `each'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:413:in `load_application_classes'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:411:in `each'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:411:in `load_application_classes'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:197:in `process'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:113:in `send'
from /Users/casper/.rvm/gems/ruby-1.8.7-p248#darebusters/gems/rails-2.3.8/lib/initializer.rb:113:in `run'
from /Users/casper/Projects/cf/darebusters/config/environment.rb:9
from /Users/casper/Projects/cf/darebusters/spec/spec_helper.rb:5:in `require'
from /Users/casper/Projects/cf/darebusters/spec/spec_helper.rb:5
from spec/models/comment_spec.rb:1:in `require'
from spec/models/comment_spec.rb:1
I'm pretty sure the problem is not related to the Inherited Resources gem - I think gems are not loaded at all at this point, and Inherited Resources just happens to be the first one needed.
The thing that's baffling me is that /activesupport-2.3.8/lib/active_support/dependencies.rb:158 requires /app/controllers/admin/base_controller during the initialization phase. It shouldn't do that, and it certaintly doesn't do it in development mode. I can comment out gem 'inherited_resources' in my Gemfile, and site still runs. It's not until I hit an action that loads a controller that use Inherited Resources I get the "uninitialized constant" error.
For clarity, here's my Gemfile and spec_helper.rb.
Even older question, but I had the same issue with Bundler 1.0.3, Rspec-Rails 1.3.3 & Rails 2.3.8 and was able to boil it down to remarkable_rails. Remarkable-Rails seems to load rspec/rails before the initializers are run by the environment.
After adding :require => nil to it in the Gemfile and requiring it by hand in spec_helper AFTER requiring the rails-environment the issue disappeared.
I realize this is an old question, but I just came across the same problem.
I had the 1.3.0 version of rspec and rspec-rails required by my application. Looking at the rspec-rails changelog, I noticed there were a few bug fixes after 1.3.0. Upgrading rspec-rails to 1.3.2 fixed everything for me.
To fix this problem I changed by Gemfile from:
gem 'rspec-rails', '~> 1.3.2', :require => 'spec/rails'
to
gem 'rspec-rails', '~> 1.3.2'
and added the following to my spec helper
require 'spec/rails'

rake spec not using the rails environment

I'm attempting to use rspec in a rails project I've just upgraded to rails 2.3.2. I've installed rspec 1.2.6 and rspec-rails 1.2.6 as plugins in the app.
My problem is the specs don't have access to my app classes or any of the rails standard libraries.
First I had to specify the model class I want to test by using the full path from RAILS_ROOT but now as it loads the class I get the following
/app/models/person.rb:1: uninitialized constant ActiveRecord (NameError)
from ./spec/models/person_spec.rb:1:in `require'
from ./spec/models/person_spec.rb:1
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `each'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/options.rb:99:in `run_examples'
from /Users/law/Projects/roster/vendor/plugins/rspec/lib/spec/runner/command_line.rb:9:in `run'
from /Users/law/Projects/roster/vendor/plugins/rspec/bin/spec:4
rake aborted!
I am launching rspec by calling rake spec from the root of the application.
Any ideas on what might be missing in this situation?
you need indeed include the spec_helper.rb in every spec file you write....
You can run individual specs that way:
$ spec specs/models/person_spec.rb
instead of always running the whole spec suite
I havn't used spec, so this may not solve your problem, but if you're writing your own rake task and need your rails environment, you have to ask for it.
task(:task_name => :environment) do
# Task Implementation Here
end

Resources