Ruby rspec test command: could not locate Gemfile - ruby

I just installed Ruby 1.9.3 on Windows 7 and I also installed rubygems. I'm trying to work with rspec so I ran:
gem install rspec
It seemed to work well and everything installed. SO I went on to try the example on this page. But anytime I run the rspec command I get this error message:
"Could not locate Gemfile".
According to the example, I should get: "./bowling_spec.rb:4:
uninitialized constant Bowling"
I've googled it and it was suggested I try bundle exec rspec but it still yielded the same results.
I have also tried the suggestion on this page but it yields the same results. What am I doing wrong? Thanks

Create the Gemfile with this content. Gemfile can have no extension or .gem extension
source 'https://rubygems.org'
gem 'rspec'
so you have
app/
Gemfile or Gemfile.gem
spec/
bowling_spec.rb
Also you might need to execute this commands after
gem install bundler
and then, in the app directory
bundle install

gem install rspec in the same directory as your app and
change require statement to require './bowling.rb'

Related

Error message when trying to create Gemfile

Can someone please break down the very very beginnings of how to run a Gemfile. I've read the Bundler webiste and it says I have to type "Gem Install Bundler" then source "https://rubygems.org". Everytime I do that I get an error message saying "Source is not recognised as an internal or external command, operable program or batch file. I've tried creating an empty gemfile first by typing "bundle init" but when I type my source, I get the error message. I also found some literature about telling it to look in github and tried to direct it to the actual project in github but it gave me the same error message.
I am completely new to coding but have to run some gems off a file in github to test my answers on some questions I've been given. There's a gemfile on the github page with this in it:
source 'https://rubygems.org'
gem rspec
gem pry
Any help for an absolute beginner is much appreciated.
You need to use source 'http://rubygems.org' not https:// which isn't supported yet
Run
gem install bundler
then you can author a Gemfile like
source 'https://rubygems.org'
gem 'rspec'
gem 'pry'
after this, you may run
bundle install
and as an outcome rspec and pry gems will be installed in your system.
You have more information about Gemfiles in Bundler official documentation.
Also, bear in mind that you are installing those gems system-wide, this might cause dependency problems and/or version problems if you have more projects where those gems are required. If that is the case, I recommend you using something to control Ruby versions and gemsets, for example RVM.

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. I have installed the gem using bundler but when I run bundle exec ./mysql_connector, I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError). Can you please help me troubleshoot what the problem is?
What I did
Installed rails in my home directory.
I do not have root access to the server so I have installed rails in my local directory by following the instructions here:
http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/
Created a directory for my application.
My application resides in my home directory in a folder called connector. It has a Gemfile that looks like this:
source 'https://rubygems.org'
gem 'ruby-mysql'
Call bundle install.
Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Add dependencies to my script. My script is in connector/mysql_connector and it reads:
#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'
Make script executable. I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Run the script. I execute using bundle exec mysql_connector and see:
/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
from /home/dcox/bin/mysql_connector:4:in `<main>'
Is it the $LOAD_PATH? After searching around for answers, I discovered a lot of SO answers as well as a blog post (https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. Here is what I see when I run $LOAD_PATH from IRB:
irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0",
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0",
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0",
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]
Next I checked to see the location of ruby-mysql:
dcox#analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14
Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right?
Any advice or help is very much appreciated! Thanks!!
If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).
Your file should call Bundler.setup http://bundler.io/bundler_setup.html
Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

Using rspec with a local gem

I am using a local gem (on my machine) with another application that is a command line app.
I have something like this in the gemfile to refer to the local gem:
gem 'mygem', :path => '/Users/devmachine/Projects/mygem'
When I run bundle console I am able to use the gem and all is well. However, whenever I run my test suite (rspec) I get the following message:
ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mygem (LoadError)
I'm confused. Any ideas?
You need to run:
bundle exec rspec
If you are not using the bundle evironment rspec won't know what you put in your Gemfile and just use the Gems it finds installed.

Bundler and wrong binstubs?

I run rails s or bundle exec rails s and I get this warning:
Bundler is using a binstub that was created for a different gem.
This is deprecated, in future versions you may need to `bundle binstub rails` to work around a system/bundle conflict.
What does this mean? From looking around the bundler site, my understanding of binstubs is that you can set executables to them, so instead of running bundle exec blabla you can just do bin/blabla. So this error is saying my bundler isn't set to the right binstub?
When I run the bundle binstub rails I get this output
rails has no executables, but you may want one from a gem it depends on.
railties has: rails
bundler has: bundle, bundler
I do not understand what my system is trying to tell me, and it's not breaking anything, but I have a hunch this could turn into a bigger issue if I don't fix it
ruby 2.0.0p247
which ruby
/Users/evan/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
which bundler
/Users/evan/.rvm/gems/ruby-2.0.0-p247/bin/bundler
Rails 4.0.2
Edit:
So, if I run the commands in the nag message:
bundle config --delete bin # Turn off Bundler's stub generator
rake rails:update:bin # Use the new Rails 4 executables
I end up getting uninitialized constant Bundler errors with bundle exec commands and the only way I've found to fix that is to rerun bundle install --binstubs which brings back the nag message at the start of this post.
What worked for me was
rm -rf bin/*
Then open a new terminal session and
bundle exec rake app:update:bin
Solution in my case: - Other solutions didn't work for me.
In your Rails directory:
mv /usr/bin/rails /usr/bin/rails.old
bundle config --delete bin
rm -rf bin
# for rails 4.x:
rake rails:update:bin
# for rails 3.x:
bundle install --binstubs
# if you're using rbenv
rbenv rehash
rm -rf ~/.rbenv/plugins/{rbenv-bundle-exec,rbenv-gemset,bundler}
Also be sure that bin/rails is added to the path like:
PATH=./bin:$PATH
Good luck.
This error may raise when you update your ruby but not the related gems.
To check if this is your case, try to make a new rails app in a new empty directory (to be sure RVM is not autoloading any gemset)
make /tmp/test && cd test && rails new test
If this fails stating that it cannot find a suitable 'rails', then simply run
gem update
and overwrite any conflicting rails.
gem uninstall bundler
gem install bundler
Uninstalling all my versions of Bundler, and then installing the latest version fixed it for me. I had multiple versions of bundler installed, so when I ran bundle exec rails s I think the wrong Bundler was used, giving me the warning message.
You may need to generate new stubs after reinstalling Bundler, but I didn't have to.
I was able to fix this by looking at the commit history for bin/rails using git log -p bin/rails
The current, error producing content is:
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'rails' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('railties', 'rails')
The original, non-error content was:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
When I restored the original bin/rails content, the warning message disappeared. Previous attempts had returned uninitialized constant Bundler errors on all bundle exec commands, but now they work. It's worth noting that the original content appears to be exactly what rails new blabla generates in rails 4.0.x.
Still, I would like to know why the first code block causes issues because it's exactly what bundle install --binstubs generates.
Edit: turns out this solution does not work. Pushed this fix to a heroku staging server and heroku errors on startup: all bin/rails commands throw uninitialized constant Bundler and heroku starts up with bin/rails server ..... so this is a not really a fix.
Edit2:
If I add these two lines to the second block (the original bin/rails content), all bin/rails commands are working again:
require 'rubygems'
require 'bundler/setup'
My guess is that the second line is what fixes the bundler errors I was having.
Interestingly, when I tried to edit the first block of code in this post to try and debug which line was throwing the warning, any change I made caused all rails commands to failure, with no information except for the nag note in the OP. weird.
Final bin/rails that fixed my issue:
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rubygems'
require 'bundler/setup'
require 'rails/commands'
Any additional insight from people who find this would be welcome!

Install gems in a Rakefile

I've searched around the Internet, what is the syntax for installing a gem in a rakefile? the only way I've been able to is to skirt around the issue and use a exec() function e.g.
cmd = "gem install geoip"
exec(cmd)
There has to be a better way for this.
gems:install
Does not seem to work for me
Bundler is used to download gems. Rake is just used to run tasks with the Ruby code itself
You would create a Gemfile, give it a source like this
source 'https://rubygems.org'
and add the gems you want like this
gem 'rails'
then run bundle in the directiory with the Gemfile and it gets all of the gems you need.

Resources