setting up Rspec - ruby

I'm trying to run rspec on my machine for the first time. I did gem install rspec in the global gemset. However, when I try to do
spec intranet_reader_spec.rb
I get
-bash: spec: command not found
And when I try to do
bundle exec rspec spec intranet_reader_spec.rb
I get
Could not locate Gemfile
(Note, I don't have a gemfile. I'm just running one test script from a ruby book)
I found these instructions for adding spec to the path, i.e. put the following in bashrc
export PATH=$PATH:/Users/<UserName>/.gem/ruby/1.8/bin
However, that didn't work. I note that on my mac. if I navigate to .gem/ruby/1.8 and do 'ls', it doesn't have a 'bin.' In the 1.8 folder, it has
cache doc gems specifications
Can anyone tell me what I might be doing wrong?

If you don't have a Gemfile then you can't run bundle exec.
Also, the command for RSpec 2.x is rspec. You might be following an outdated tutorial, since the command was spec in version 1.x.
Just run:
rspec intranet_reader_spec.rb

Related

Ruby rspec test command: could not locate Gemfile

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'

bash: spec: command not found

I installed rspec gem. I got following message
Successfully installed rspec-2.11.0
1 gem installed
Installing ri documentation for rspec-2.11.0...
Installing RDoc documentation for rspec-2.11.0...
but when I run
spec path/to/file.rb
I get
-bash: spec: command not found
Any solution on this?
I think you should run
rspec path/to/file.rb
First try to use rspec instead of just spec
Secondly, try this
export PATH=$PATH:/path/to/gems/bin
obviously replace the dummy path with the correct one, pointing to the bin dir inside your gem instalation path. For example, mine is ~/.rvm/gems/ruby-1.9.3-p194/bin

Why do I need bundle exec to require this rubygem?

I have a Rails app with some non-Rails-dependent files under `lib/services'. One of these files uses the Domainatrix gem.
require "domainatrix"
class SuggestionParser
# various suggestion parsing methods
end
I have an empty spec for this file under spec/lib.
require "services/suggestion_parser"
describe SuggestionParser do
end
Unfortunately, when I try to run that spec without bundle exec I hit an error:
$: rspec spec/lib/services/suggestion_parser_spec.rb
-> /Users/davidtuite/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require': cannot load such file -- domainatrix (LoadError)
Every other spec and gem in my project will run without using bundle exec. Why do I need to prefix this one in order to get it to run?
For convenience, here's a link to the Domainatrix gemspec.
My guess would be that domainatrix is declared using the :path or :git options in the Gemfile, neither of which install the gem in a way that makes it accessible to rubygems.
This could be confirmed if you post the line for domainatrix from the Gemfile.
try running the following commands:
$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ bundle install --without production --binstubs=./bundler_stubs
This won't solve the specific problem with your gem, but it will take away the necessity to type in bundle exec every time you run your tests if you're using rvm.

An easy way to run tests on a gem?

Is there a quick and easy way to test a gem which is already installed locally? Like:
gem test gem_name_to_test
rubygems docs says one can put gem: --run-tests in ~/.gemrc file to run unit tests when a gem is installed. I could not make it work though and that is not exactly what I need.
You can navigate to the place the gem lives and run tests from there, so for example:
$ cd ~/.rvm/gems/ruby-1.9.2-p290/gems/awesome_print-0.4.0
$ rake spec
Note that additional dependencies may need to be installed via bundler or gem
There is "gem test" command, which may or may not be what you are looking for. It run tests agains the package and sends them to test.rubygems.org.
gem install rubygems-test
gem test gem_name_to_test

How to use bundler gem binaries in path

I just started using bundler for gem packaging in vendor/. The problem is with certain gems (like rspec and cucumber) that have binaries. The binary path that is under my_app/vendor/gems/ruby/1.8/...cucumber-0.6.2/bin/ is not in my path, therefore when I go to run cucumber i get command cannot be found.
What is the easiest way to execute the bundled gem binaries from within the app rather than adding a large number of folders to my path?
Thanks
Newer version of bundler have an "exec" action. So for cucumber it would be:
bundle exec cucumber
OK, so symlinking was in fact a daft idea. This question did get me thinking though, and I found this: http://litanyagainstfear.com/blog/2009/10/14/gem-bundler-is-the-future/
Bundler will also dump gem executables in your Rails.root/bin directory. This means you can then use bin/rake, for example.
so, from the Rails root, does bin/cucumber work?

Resources