Rails 3 - How do I get Rspec spec/ & autotest to work? - ruby-on-rails

I was all setup with Rspec and Autotest before I updated to Rails 3.
When running rspec spec/ I'm getting:
* spec/spec.opts is deprecated.
* please use ./.rspec or ~/.rspec instead.
I tried changed spec.opts to spec.rspec and running rspec spec/ again. But now I get a whole mess of warnings:
DEPRECATION WARNING: Use toplevel init.rb; rails/init.rb is deprecated:
DEPRECATION WARNING: reset_javascript_include_default is deprecated. Please manipulate config.action_view.javascript_expansions[:defaults] directly.
DEPRECATION WARNING: RAILS_ENV is deprecated. Please use ::Rails.env.
DEPRECATION WARNING: Use toplevel init.rb; rails/init.rb is deprecated:
DEPRECATION WARNING: You are using the old router DSL which will be removed in Rails 3.1. Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/.
Autotest isn't working either. I'm getting:
.rvm/rubies/ruby-1.9.2-p0/bin/ruby -I.:lib:test -rubygems -e "['test/unit', 'spec/integration/layout_links_spec.rb', 'test/unit/color_test.rb', 'test/functional/products_controller_test.rb', 'spec/integration/users_spec.rb', 'test/functional/colors_controller_test.rb', 'test/unit/product_test.rb', 'test/unit/helpers/products_helper_test.rb', 'test/unit/helpers/colors_helper_test.rb'].each { |f| require f }" | .rvm/gems/ruby-1.9.2-p0/gems/autotest-4.3.2/bin/unit_diff -u
:29:in require': no such file to load -- spec_helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:inrequire'
from spec/integration/layout_links_spec.rb:1:in <top (required)>'
from <internal:lib/rubygems/custom_require>:29:inrequire'
from :29:in require'
from -e:1:inblock in '
from -e:1:in each'
from -e:1:in'
Any ideas?

The deprecation warnings are because of your upgrade to Rails 3, not because of RSpec. Rails 3 has a very different architecture, and the API has also changed significantly. Have you run your project through the rails_upgrade plugin? Also, check out the Rails 3 Release Notes for help on fixing the parts of your application that rely on deprecated functionality.
The second issue is probably because ruby 1.9.2 no longer puts the current directory on the load path. You'll need to manually include spec/ into the load path from your spec.opts.

Related

Unable to run RSPec using RubyMine. RSpec running fine with ubuntu terminal

I am facing issue with running RSpec using RubyMine. I am getting the following error whenever I attempt to run test with RubyMine. But if I use normal Ubuntu terminal, things all working fine.
Do anyone know why I am getting this error? If there is any gem issue or any path issue, then I should not be able to run it via terminal also right?
/bin/bash -c "/home/meowcat/.rvm/bin/rvm ruby-2.6.6 do /home/meowcat/.rvm/rubies/ruby-2.6.6/bin/ruby /home/meowcat/.rvm/gems/ruby-2.6.6/bin/rspec /home/meowcat/project/myProject/spec/api/v1/api_controller_spec.rb --require teamcity/spec/runner/formatter/teamcity/formatter --format 'Spec::Runner::Formatter::TeamcityFormatter' --example ApiController"
Testing started at 2:27 AM ...
Warning! PATH is not properly set up, /home/meowcat/.rvm/gems/ruby-2.6.6/bin is not at first place.
<log>Usually this is caused by shell initialization files. Search for <code>PATH=...</code> entries.
You can also re-add RVM to your profile by running: <code>rvm get stable --auto-dotfiles</code>
To fix it temporarily in this shell session run: <code>rvm use ruby-2.6.6</code>
To ignore this error add <code>rvm_silence_path_mismatch_check_flag=1</code> to your <code>~/.rvmrc</code> file.
An error occurred while loading spec_helper. - Did you mean?
rspec ./spec/spec_helper.rb
Failure/Error: require 'mongoid-rspec'
Gem::ConflictError:
Unable to activate mongoid-5.4.1, because activemodel-5.0.7.2 conflicts with activemodel (~> 4.0)
# /home/meowcat/.rvm/gems/ruby-2.6.6/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `block in require'
# /home/meowcat/.rvm/gems/ruby-2.6.6/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:259:in `load_dependency'
# /home/meowcat/.rvm/gems/ruby-2.6.6/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `require'
# ./spec/spec_helper.rb:12:in `<top (required)>'
# ------------------
# --- Caused by: ---
# LoadError:
# cannot load such file -- mongoid-rspec
# /home/meowcat/.rvm/gems/ruby-2.6.6/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `block in require'
Run options: include {:full_description=>/ApiController/}
All examples were filtered out
My Conf:
It seems like it invoked the rspec command directly without using bundle exec and hence the gem dependency wasn't handled correctly.
You can try enable running your rspec in Rubymine with bundler context enabled so that the gems can be loaded correctly. See attached image for the configuration.
Rubymine Rspec config screen

Trouble with new rails app

I have just created a new rails app (on the CL, using rails new), I am on 4.2.6, but it seems like before I can do anything to the app that i've hit errors.
first...
/config/environments/development.rb:53:in `block in <top (required)>':
uninitialized constant ActiveSupport::EventedFileUpdateChecker (NameError)
then once I comment that out...
/config/initializers/new_framework_defaults.rb:15:in `<top (required)>':
undefined method `to_time_preserves_timezone=' for ActiveSupport:Module (NoMethodError)
and once that is commented out...
/config/initializers/new_framework_defaults.rb:21:in `<top (required)>':
undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module (NoMethodError)
and lastly...
.gem/ruby/2.2.3/gems/actionmailer-4.2.5/lib/action_mailer/base.rb:569:in `method_missing':
undefined method `perform_caching=' for ActionMailer::Base:Class (NoMethodError)
everything I can turn up on Google suggests these are Rails 5 related things. I'm not sure how to get around them, or how to create an app that is still specific to 4.2.6.
If still you want to use Rails 5 comment the below line in development.rb file:
config.action_mailer.perform_caching = false
And after doing that you may get:
ActionView::Template::Error (couldn't find file 'ation_cable' with type 'application/javascript'
Then remove = from //= require action_cable in your cable.js file.
tl;dr: Re-create your Rails project, providing the specific Rails version.
To elaborate on Peter's answer: often this error occurs because you have a newer version of Rails installed than the one called for in your .railsrc or template.rb.
If this is the case, when you run rails new my_new_app, the newest version is used by default for the earlier steps of the process, but then once the template/railsrc version is installed, latter steps use this version. This causes compatibility issues.
You can verify that this is your problem by comparing the output of rails -v (in the directory where you called rails new) with what's in your .railsrc or template.rb. If they are different, there is an easy fix:
Recreate your Rails app, specifying the same Rails version from your .railsrc or template.rb in the command line call:
rails _4.2.5.1_ new my_new_app

`rake spec` fails with no error message

OS X Mavericks
Ruby 1.8.7
Rails 2.3.18
Rspec 1.3.2
Rspec-rails 1.3.4
Output of rake spec
$ rake spec
(in /Users/timosand/Dropbox/Development/epassi)
Please install RDoc 2.4.2+ to generate documentation.
DEPRECATION WARNING: Rake tasks in vendor/plugins/annotate_models/tasks, vendor/plugins/barcode_generator/tasks, vendor/plugins/crypted_authentication/tasks, vendor/plugins/oauth-plugin/tasks, vendor/plugins/tarantula/tasks, and vendor/plugins/ym4r_gm/tasks are deprecated. Use lib/tasks instead. (called from /usr/local/var/rbenv/versions/1.8.7-p375/lib/ruby/gems/1.8/gems/rails-2.3.18/lib/tasks/rails.rb:10)
DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths instead. (called from /usr/local/var/rbenv/versions/1.8.7-p375/lib/ruby/gems/1.8/gems/carrierwave-0.4.10/lib/carrierwave.rb:84)
/Users/timosand/Dropbox/Development/epassi/config/initializers/oauth_consumers.rb:7: warning: already initialized constant VERSION
/Users/timosand/Dropbox/Development/epassi/lib/spreadsheet/format.rb:21: warning: already initialized constant COLORS
I have 45 spec files
$ find spec -name '*_spec.rb' | wc -l
45
I've added p __FILE_ to each spec and every filename gets printed. Still the actual tests are not run.
Any advice on how to solve this?
Here is my spec_helper.rb and one controller spec as an example

How to Stop RSpec Warning Messages

I have just started learning to use RSpec on my rails application.
It all seems to work ok, but when I run rspec spec I get pages and pages of what I think are lint messages.
I don't mind the ones that refer to my code, but lots of them refer to Gems that I am using.
I can't really fix those.
How can I configure RSpec to apply lint only to my code and not external Gems?
Here is a small sample. I get over 2000 lines of this stuff.
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery/model.rb:265: warning: method redefined; discarding old username_attribute_names=
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-3.2.18/lib/active_support/dependencies.rb:251: warning: loading in progress, circular require considered harmful - /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery.rb
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `load'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
from /Users/jcreasey
This is my .rspec file
-color
--warnings
--require spec_helper
If you have /.rspec file remove --warning option from it.

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