ruby on rails rspec error - ruby-on-rails

I'm following through the "Learn Rails by Example" book, and I'm trying to run the tests. For some reason I can't get rspec to work properly.
If I run the rspec spec/ command as he instructs, I get the following error:
$ rspec spec/
/home/desktop/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.21/lib/bundler/runtime.rb:31:in `block in setup':
You have already activated rspec-core 2.7.1, but your Gemfile requires rspec-core 2.6.4.
Using bundle exec may solve this. (Gem::LoadError)
The odd thing is my Gemfile doesn't specify version--
group :development do
gem 'rspec-rails'
end
group :test do
gem 'rspec'
gem 'webrat'
end
If I follow the advice from the error message and use bundle exec rspec spec/ then the first two tests pass-- but the new "about" page we built in the tutorial fails with the following error, even though as far as I can tell the page I'd built (and controller actions etc.) are exactly as they should be:
Failures:
1) PagesController GET 'about' should be successful
Failure/Error: response.should_be_success
NoMethodError:
undefined method `should_be_success' for #<ActionController::TestResponse:0x00000003539438>
# ./spec/controllers/pages_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
Finished in 0.10861 seconds
3 examples, 1 failure
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:21 # PagesController GET 'about' should be successful
I'm a pretty experienced programmer but I've run into endless issues with conflicting gem versions and a hundred different ways to accomplish all the different tasks using Rails (eg. "use RVM", "Don't use RVM", "install gems using sudo", "don't install gems using sudo" etc.)
My dev machine is running ubuntu linux.
Thanks for any help-- please explain if you would what I'm doing wrong in Ruby noob language!

Running bundle exec is correct, and is needed because you have a newer version of that gem installed that gets loaded instead of the one specified in your Gemfile.lock. Using bundle exec overrides the load path, causing only the gems specified in your Gemfile.lock to be loaded. (You may find it handy to alias bundle exec to something shorter.)
The answer to the second problem is right in the error messages:
undefined method `should_be_success'
it should be should be_success.

Related

minitest_plugin.rb:9 getting wrong number of arguments

~/Sites/sample_app$ rails test
Running via Spring preloader in process 24338
Run options: --seed 58780
Running:
..
Finished in 0.292172s, 6.8453 runs/s, 6.8453 assertions/s.
/var/lib/gems/2.3.0/gems/railties-5.1.0/lib/rails/test_unit/minitest_plugin.rb:9:in `aggregated_results': wrong number of arguments (given 1, expected 0) (ArgumentError)
I don't understand why I am getting this error. I Can't seem to find anyone with this specific error. I'm following the tutorial https://www.railstutorial.org/book/static_pages. This error follows the rails test command. Running Ubuntu and rails 5.1 if that helps. I'm not passing any arguments so I don't understand why I am getting this error.
My test file looks like :
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help_url
assert_response :success
end
This is actually a bug in rails, revealed by minitest update to 5.10.2 as said here and it has already been fixed here.
As the pull request is only 10 hours old (at the time of writing), it has not yet been released, although it's already merged.
In the mean time, you can specify in your Gemfile:
gem 'minitest', '~> 5.10', '!= 5.10.2'
Edit
Don't forget to bundle update minitest
Hey I'm doing this exact tutorial and followed the top solution and it fixed my issue, specifically (for us total noobs) I did this to my Gemfile
group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
gem 'minitest', '~> 5.10', '!= 5.10.2' # add this here to fix error
end
It turns out that in my test/test_helper.rb I needed a line of code that was missing. I added this before "class ActiveSupport::TestCase".
Minitest::Reporters.use!
This gave me a passing result for my test with no strange argument error. Hope this helps someone for the future!
Problem is in minitest 5.10.2. Downgrading to 5.10.1 fixes it.
Add this line in test group in Gemfile
gem 'minitest', '5.10.1'
Then run following commands
bundle update minitest
rails test
The issue is resolved
I also encountered this error. I tried using the top voted solution, but rails told me that bundle has locked minitest to 5.10.2. Instead, I just did a gem update minitest and my tests ran without the weird error.

Why is this gem not adding rake tasks to a Rails app?

We have a gem which runs via a Rake task. The task is defined in lib/tasks/<namespace>.rake. After reading Rake tasks inside gem not being found I confirmed that the .gemspec includes the file defining the task; there is also a railtie which should be including the tasks as suggested in including rake tasks in gems. And yet our Rails 4.1 application doesn't seem to load the Rake task.
What am I missing?
I just successfully tested your gem and I can see no problem with the rake tasks in it:
I added gem_fresh to the Gemfile and ran bundle, the gem installed
Immediately I could see the rake present in the list of rakes:
$ rake -T outdated
rake gem_fresh:outdated # outdated
Then I updated the railtie.rb file to use the load method to load the rake defined in lib/task/metrics.rake and searched the available rakes again:
# lib/gem_fresh/railtie.rb:
rake_tasks do
namespace :gem_fresh do
desc "outdated"
task :outdated => :environment do
GemFresh::Reporter.new.report
end
end
load "tasks/metrics.rake"
end
$ rake -T outdated
rake gem_fresh:outdated # outdated
rake metrics:outdated_gems # display outdated gem version metrics
So, I can see no problem with your gem. Both methods in railtie (inline rake as well as using the load method) seem to work OK. The only difference I noticed is that I tested this on rails 4.2 but I rather doubt that would make a difference.
Did you put the gem into your Gemfile? If I remove it from there, I indeed see no gem_fresh rakes defined.
The problem was not with the gem, but with the way it was included in the app.
In the Gemfile, this works and includes the rake task:
gem 'gem_fresh'
This works but doesn't include the rake task:
group :development do
gem 'gem_fresh'
end
This seems to be due to how Bundler requires gems in Rails apps. From the documentation:
By default, a Rails generated app calls Bundler.require(:default, Rails.env) in your application.rb, which links the groups in your Gemfile to the Rails environment.
If for some reason that Rails.env argument wasn't evaluating to include the :development group, which seems to be the case when I call rake -T, the gem wouldn't be Bundler.require-d, and the rake tasks wouldn't be loaded.
The fact that it isn't including the :development group seems to be an odd Bundler "gotcha", but at least now I know that moving it to the default group solves the issue and it's not a problem with the gem.
in my case, the project requiring the gem had a rake file, and the gem I was requiring had the same name when I changed the name of the rake file I could see the tasks in the project.
my_gem had lib/tasks/XXXX.rake and my_proj also had lib/tasks/XXXX.rake,
after I changed my_gem XXXX.rake to YYYY.rake I managed to list and use the tasks in YYYY.rake

Problems with Rails Girls Guides's Adding Authentication with Devise

I'm very new to Rails, and I'm trying to build a user authentication system with 'devise', but I am failing. I'm following Rails Girls Guides's tutorial on adding authentication using the gem 'devise'. Once I got to step 5, which is to set up the User model, I received this error after running rake db:migrate
rake aborted!
NoMethodError: undefined method `merge!' for #<ActionDispatch::Routing::Mapper::Scope:0x007fd3397a7448>
/Users/.rvm/gems/ruby-2.1.2/gems/devise-3.0.4/lib/devise/rails/routes.rb:420:in `ensure in with_devise_exclusive_scope'
/Users/.rvm/gems/ruby-2.1.2/gems/devise-3.0.4/lib/devise/rails/routes.rb:420:in `with_devise_exclusive_scope'
/Users/.rvm/gems/ruby-2.1.2/gems/devise-3.0.4/lib/devise/rails/routes.rb:233:in `block (2 levels) in devise_for'
/Users/.rvm/gems/ruby-2.1.2/gems/devise-3.0.4/lib/devise/rails/routes.rb:336:in `block in devise_scope'
Has anyone solved this error? I've looked at all solutions such as not generating user model before installing devise, running bundle install, running rails g devise:install, and more. Nothing has worked so far when I'm trying to use devise for Rails 4.0.
Also, does anyone have recommendations on what tutorials I can follow to build an authentication system? I've tried a lot so far, and none has worked.
This looks to be a know bug with prior 3.4.0 devise.
Upgrading it should make this error go away.
To do that edit your Gemfile where you have gem "devise" <might have some other stuff in front> and change it to gem "devise", '~> 3.4.1'
then run bundle update devise and you are done.

Error when trying to run rspec: `require': cannot load such file -- rails_helper (LoadError)

I am trying to run rspec for Ruby on Rails. I am running Rails 4.1.1. I have installed the gem, have established a spec folder with some tests. I have created a directory through $ rails g rspec:install
I tried to create a testing database through $ rake db:test:prepare but it throws this error message:
WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test
schema automatically, see the release notes for details.
So I ended up looking at this stack overflow post, and of the two options, the one that worked was:
rake db:schema:load RAILS_ENV=test
So, now I need to run rspec.
When I run $ rspec spec from the command line I get this error:
/Users/myname/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:55:in `require': cannot load such file -- rails_helper (LoadError)
How do I resolve this so that I can start running tests?
There is some problem with rspec V3. But in your case you are using V2.
change
require 'rails_helper'
to
require 'spec_helper'
Other description find here https://teamtreehouse.com/forum/problem-with-rspec
For V3 :
If someone using rspec V3 then similar error occurs when generator not run. So before trying anything run generator.
rails generate rspec:install
If you are getting a huge list of warning on your console. Then you need to remove --warnings from .rspec file.
I actually just had this error on rails 4 with rspec 3, but in my case I forgot to run the generator:
rails generate rspec:install
Also I had to remove warnings from .rspec, as stated by one of rpsec developers
For me, the problem was due to a different "rspec-support" version loaded and activated. I solved by prepending a "bundle exec":
bundle exec rspec
Always remember to run the
rspec or the bundle exec rspec from the root of your project.
Something like:
bundle exec rspec spec/features/file_test_spec.rb
For newer versions (3.5), if you need to run the generator, rails generate rspec:install doesn't work for me. I used:
rspec --init
Creates .rspec and spec/spec_helper.rb.
EDIT:
Just in case, I found out that I installed rspec gem for ruby, not the one for Rails, so rspec:install wasn't available. Should use https://github.com/rspec/rspec-rails.
Sometimes you need to run rspec command from Project's parent directory
Please install the following gem:
gem install 'rspec-rails'
And then run:
rails generate rspec:install
This will generate the spec folder and rails_helper.rb and spec_helper.rb. Your error should be taken care once this is done.
None of these suggestions worked for me on Rails 4.2.0 with rspec-rails 3.2.3.
Instead, I placed the following two lines at the top of my spec file:
require 'rails_helper'
require 'spec_helper'
Now I can execute rspec spec/features/my_spec.rb.
I had the same error but there was no reference to rails_helper anywhere and we weren't even using rails.
However, I eventually found that my ~/.rspec was requiring rails_helper (presumably from creating rails app in the past) which was causing the issue ...
$ cat ~/.rspec
--color
--format documentation
--require spec_helper
--require rails_helper
The fix is as simple as removing that line. Hope this helps someone else grappling with the same issue!
One possibility that worked for me is to make sure you're requiring spec_helper. RubyMine can default require rspec and this can sometimes lead to this error.
You may also have your file named incorrectly. Make sure it ends in _spec.rb. I had misspelled my file to end in _soec.rb and was scratching my head for a while...
I got the error you described when running a controller test. However, when I ran a model test, I got the true error. it ended up saying that the error was occuring because my database wasn't migrated. I fixed the error by running this command...
rake db:test:prepare
For someone. If you got that error when you trynna test a specific file. you might can like this
$ bundle exec rspec ./spec/features/user_login_spec.rb
$ bundle exec rspec ./spec/models/user_login_spec.rb

What does error occurred while evaluating nil.dependencies mean?

I'm running ruby on rails v2.2.2 and when I run rake gems I get this error.
$ rake gems --trace
- [I] settingslogic
rake aborted!
You have a nil object when you didn't expect it!
The error occured while evaluating nil.dependencies
/vendor/rails/railities/lib/rails/gem_dependency.rb:77:in `dependencies'
I figured out what was going on. There is a bug in the core rake gems function where it assumed there were specifications for the gem and would error out when it didn't exist for that gem.
Here is the ticket for this issue with a working patch
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1464
Could be a problem with a gem it's trying to load, or your setup of required gems. Check config/environment.rb for any odd-looking config.gem lines, and do an update of all your gems (sudo gem update).
There is a bit more detail here with two alternate workarounds
-add one line to the dependency code in rails
or
-specify gem dependencies before the gem

Resources