I am creating an application on ruby on rails:
After creating users, for validating users I am running below commands:
$ bundle exec rake db:migrate
$ bundle exec rake test:prepare
Both command are run properly on the rails commandLine, but when I run the below command:
$ bundle exec rspec spec/models/user_spec.rb
I am getting error below:
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
And I am using 4.1.1 version of rails.
I don't understand why it comes.
Kindly suggest me, waiting for your reply.
Thanks.
More a note for posterity than anything, but this issue can also happen if you're using an older version of shoulda-matchers. You can see some discussion around this on their Github repo here, or on the rspec-rails repo here.
Update shoulda-matchers by running bundle update shoulda-matchers. You want the latest version (or at least 2.6.2) and this message should go away.
Include minitest gem in your 'Gemfile' and run bundle install
gem install 'minitest'
and then bundle install
Related
I am new to rails, using Michael Hart's book on Rails tutorial for my learning. I've done gem install bundler and also bundle install to get all my gems specified in the gemfile;
But when I run bundle exec rake test to test my application it shows system cannot find the path specified.
I've written my test code in controller_test.rb with require test_helper included as part of the test code.
How can I fix this issue?
I'm attempting (without much success) to run rake db:migrate on a rails project, however it returns:
Could not find rake-10.4.2 in any of the sources
Run bundle install to install missing gems.
I ran bundle install and worked fine - installed rake 10.4.2, however when I ran: rake --version (for some reason you can't do rake -v ???) and it shows: rake, version 0.9.6
I ran bundle update rake and returned my list of gems and then: Your bundle is updated!
Why isn't rake updating? Is there something I'm doing wrong (I'm new to rails btw - so this is probably really simple)
Any help is really appreciated
TL; DR: gem install rake -v '10.4.2'
So, I had this problem myself. I wanted to know why rails s would work yesterday and not today.
First I checked if I was in the correct directory, I was.
Then I re-ran bundle install to make sure rake was getting installed. It was, and I was able to see it in my Gemfile.lock
So I figured my gemset might be corrupt (I use RVM). rvm gemset empty then bundle install
Still, whenever I ran rails s, I got this error. bin/rails s worked, as well as bundle exec rails s. But I don't want a prefix, I want rails to work (It works in other rails projects)
Eventually I tried gem install rake and it worked! I recommend adding -v '10.4.2' to the command so that you get the correct rake version. Now when I which rake, I get the gemset for my project: "/Users/cm022291/.rvm/gems/ruby-2.2.1#project_gemset/bin/rake"
and when I run rails s the server starts successfully.
Try typing
bundle exec rake db:migrate
That will ensure that the Rake being invoked is the one you've bundled.
Background:
I get errors whenever I run rake in an older project. (uninitialized constant Rake::DSL).
The rails project in question is an old project that was started with Rails 2.1 (I think), and since then I've updated the OS on my laptop a couple of times, and made updates along the way to make it run.
Right now, the rails app works fine, provided I have RAILS_GEM_VERSION set to 2.3.5. I'm not sure if the app was completely updated to Rails 2.3.5.
There is no Gemfile in my older project.
If I create a brand-new rails project (and unset RAILS_GEM_VERSION), rake runs fine.
My question: To troubleshoot, I'd like to try newer versions of rake. I'd like to know how to force one specific version to be used, since it appears I have multiple versions installed.
Info on my environment:
$ gem list rake
*** LOCAL GEMS ***
rake (0.9.2.2, 0.9.2, 0.8.7, 0.8.3)
$ rake --version
rake, version 0.8.7
So it looks like it's picking up the 0.8.7 version.
All the help files online seem to tell me to specify the rake version in the Gemfile, but there isn't one in this project. (Maybe it predates gemfiles?)
If I unset the RAILS_GEM_ENVIRONMENT variable altogether, and try to run rake, I get:
rake aborted!
can't activate rails (= 2.3.5, runtime) for [], already activated rails-3.2.8 for []
None of the environment config files in my older project set that variable either.
This may be of help. Have you tried the underscore solution?
Example:
rake _0.9.2_
you can run rake specific version by using this
bundle exec rake ...
more detail see this - bundle exec, rake
You can uninstall the current version of rake and install another desired version using commands similar to the following:
gem uninstall rake 12.3.1
gem install rake 10.5.0
(Note: you might need to run as sudo for permissions)
I had a problem where I received the following error while trying to install rake 10.5.0:
Could not find a valid gem '0.8.7' (>= 0) in any repository
I resolved this problem by adding the following line to my Gemfile:
gem 'rake', ' <11.0'
After editing Gemfile I was able to successfully downgrade rake by updating my gems:
bundle update
My question has already been asked here, but I am trying to understand the reasons behind it as opposed to how to work around it.
The error I got was;
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)
Now I have been given various solutions like using "mpapis-bundler", or to create a shorthand for "bundle exec", but I was under the impression that that was what
$bundle install --binstubs
was for.
More specifically, since I have no version numbers stated in my gemfile for rspec-rails, why do I have this incompatibility? My error also occurred when I tried
$rake db:migrate
telling me that
You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Consider using bundle exec.
Any explanations would be appreciated.
EDIT:
All my gems for my app are in a gemset, and I have updated my gems again. Should an update not make sure that related gems are compatible?
This happens when you install more recent gems in your system than the one in your Rails app.
Bundler simply tells ou that you must stick with those your Gemfile states.
This is the purpose of running:
bundle exec rake db:migrate
-> running the very same rake version your Gemfile provides.
Concerning updating gems from gemfile, simply do:
bundle update
The easiest way to avoid this kind of boring stuff is to isolate your gems by creating gemsets. I use RVM for this purpose.
Regarding the rake version 0.9.2.2, either ways to do is create a new gemset for the project and maintain the gem version matching your Gemfile.
For instance if there are two rake gem containing versions 0.9.2 and 0.9.2.2, specifying rake version '0.9.2' though installs, but does not run any tasks apart from blowing error saying
'You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. Using bundle exec may solve this.'
I expect bundle install to lock the gem version in Gemfile.lock and pick the rake 0.9.2, but it looks in the gemset, where by default rake 0.9.2.2 is enabled.
Just reminding the purpose of bundle install from agile web development with rails book,
'bundle install will use the Gemfile.lock as a starting point, and install only the
versions of the various gems as specified in this file. For this reason, it is
important that this file gets checked into your version control system, as this
will ensure that your colleagues and deployment targets will all be using the
exact same configuration.'
but it doesn't work that way,
The better is to uninstall rake 0.9.2.2 and use rake 0.9.2 or, use bundle update rake, that updates the rake version in Gemfile.lock to 0.9.2.2
As #apneadiving said, running "$bundle install" updates all your bunldes. However, after running "$bundle install --binstubs" I still got errors for incompatible gems whenever I omitted the "bundle exec" part.
Subsequently I needed to update my Gemfile as I added another gem, and now they work. I'm assuming the incompatibilities were solved by the gem creators.
What does bundle exec rake db:migrate mean? Or just bundle exec rake <command> in general?
I understand that bundle takes care of maintaining things in the Gemfile. I know what the word "exec" means. I understand that rake maintains all the different scripty things you can do, and I know that db:migrate is one of those. I just don't know what all these words are doing together. Why should bundle be used to execute rake to execute a database migrate?
bundle exec is a Bundler command to execute a script in the context of the current bundle (the one from your directory's Gemfile). rake db:migrate is the script where db is the namespace and migrate is the task name defined.
So bundle exec rake db:migrate executes the rake script with the command db:migrate in the context of the current bundle.
As to the "why?" I'll quote from the bundler page:
In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.
However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.
You're running bundle exec on a program. The program's creators wrote it when certain versions of gems were available. The program Gemfile specifies the versions of the gems the creators decided to use. That is, the script was made to run correctly against these gem versions.
Your system-wide Gemfile may differ from this Gemfile. You may have newer or older gems with which this script doesn't play nice. This difference in versions can give you weird errors.
bundle exec helps you avoid these errors. It executes the script using the gems specified in the script's Gemfile rather than the systemwide Gemfile. It executes the certain gem versions with the magic of shell aliases.
See more on the man page.
Here's an example Gemfile:
source 'http://rubygems.org'
gem 'rails', '2.8.3'
Here, bundle exec would execute the script using rails version 2.8.3 and not some other version you may have installed system-wide.
This comes up a lot when your gemfile.lock has different versions of the gems installed on your machine. You may get a warning after running rake (or rspec or others) such as:
You have already activated rake 10.3.1, but your Gemfile requires rake 10.1.0. Prepending "bundle exec" to your command may solve this.
Prepending bundle exec tells the bundler to execute this command regardless of the version differential. There isn't always an issue, however, you might run into problems.
Fortunately, there is a gem that solves this: rubygems-bundler.
$ gem install rubygems-bundler
$ $ gem regenerate_binstubs
Then try your rake, rspec, or whatever again.
It should probably be mentioned, that there are ways to omit bundle exec (they are all stated in chapter 3.6.1 of Michael Hartls Ruby on Rails Tutorial book).
The simplest is to just use a sufficiently up-to-date version of RVM (>= 1.11.x).
If you're restricted to an earlier version of RVM, you can always use this method also mentioned by calasyr:
$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ bundle install --binstubs=./bundler_stubs
The bundler_stubs directory should then also be added to the .gitignore file.
A third option is to use the rubygems-bundler gem if you're not using RVM:
$ gem install rubygems-bundler
$ gem regenerate_binstubs
When you directly run the rake task or execute any binary file of a gem, there is no guarantee that the command will behave as expected. Because it might happen that you already have the same gem installed on your system which have a version say 1.0 but in your project you have higher version say 2.0. In this case you can not predict which one will be used.
To enforce the desired gem version you take the help of bundle exec command which would execute the binary in context of current bundle. That means when you use bundle exec, bundler checks the gem version configured for the current project and use that to perform the task.
I have also written a post about it which also shows how we can avoid using it using bin stubs.
I have not used bundle exec much, but am setting it up now.
I have had instances where the wrong rake was used and much time wasted tracking down the problem. This helps you avoid that.
Here's how to set up RVM so you can use bundle exec by default within a specific project directory:
https://thoughtbot.com/blog/use-bundlers-binstubs
It means use rake that bundler is aware of and is part of your Gemfile over any rake that bundler is not aware of and run the db:migrate task.