Difficulty installing RSpec on Windows - ruby

I'm trying to get started with RSpec. I already had Ruby 1.8.7 installed on my Windows 7 machine.
So I typed gem install rspec and that seemed to work. But if I type spec in the command line, the command is not found. My path currently includes the bin folder in my RUBY_HOME.
If I look into the C:\Users\Eric\.gem\specs\rubygems.org%80\quick\Marshal.4.8 directory, I do see four RSpec files such as rspec-core-2.5.0.gemspec. Nevertheless, the spec command fails even in this directory.
What needs to be done to install RSpec correctly? It would seem like a path issue, but I have been unable to find a directory where the spec command works, so I can't figure what to add to my path.

On Windows, try this bundle exec rspec spec

There's no spec command in RSpec 2. Try rake spec or rspec spec.

Related

Trying to understand the error

I was learning about the gem Rspec through a tutorial when this error came up.The last thing I typed in was
$ rspec spec spec\hello_world_spec.rb
I had only installed the Rspec gem and nothing else.
the output message from the cmd
Try to get rid of spec
rspec spec\hello_world_spec.rb
You're passing spec and spec\hello_world_spec.rb as arguments to rspec. These are interpreted as files to run, or directories to search through for files to run. Since you're already running in the spec\ directory, rspec is looking for spec\spec\ and spec\spec\hello_world_spec.rb, which don't exist. Try running that from one directory up (in a typical ruby project, the "root" of your project) and it should run.
i.e. Instead of:
\rspec_tutorial\spec>rspec spec spec\hello_world_spec.rb
try:
\rspec_tutorial>rspec spec spec\hello_world_spec.rb
Also, as #Ursus points out, running rspec spec spec\hello_world_spec.rb is redundant. Rspec will search through spec\ for files to run and will run hello_world_spec.rb automatically since it's under spec. If you only want to run hello_world_spec.rb–which seems to be your intent–then drop the spec from the command, per #Ursus' answer.

RSpec basics: bin/rspec --format doc

I've installed RSpec on a win7 lappy and am following along the http://rspec.info/ homepage tutorial to make sure everything works. If I am reading their demo correctly bin/rspec --format doc should run the specification test file.
Instead, I get a system prompt for a text editor... ? I am confused.
Any explanation of what is going on or guidance about how to get my RSPEC configuration working in accordance to the makers homepage would be great.
FWIW Ruby 2.2.5p319, Bundler version 1.13.1 and gem -v tells me 2.6.7 (originally I had 2.4 but that is broken on windows, so I upgraded according to http://guides.rubygems.org/ssl-certificate-update/) Also, I have basic RSpec functionality and have completed the tutorial here: https://www.tutorialspoint.com/rspec/rspec_writing_specs.htm
Ah, I figured out what I need to do... I just need to explicitly call ruby:
ruby bin/rspec --format doc
...and the test gets run - YaY!
Per #JörgWMittag, I confirmed my Environment Variable Path to make sure ruby.exe was in there (C:\Ruby22\bin;).
Then looking at my Program Defaults, I thought that maybe I could tell win7 to associate any file named "rspec" with ruby.exe per https://support.microsoft.com/en-us/help/18539/windows-7-change-default-programs ...but I couldn't actually add file type "extensions" or "protocols" - I could only change the association of existing ones, but .rb and .rbw were in there... Maybe there is a way to do this manually, but I am not a windows expert.
Thinking on all this it occurred to me that I just needed to explicitly tell ruby to ingest the command... Heh.
I apologize if this is off-topic.

Automatically run RSPec when plain-old Ruby (not Rails) files change

I am writing a Ruby script designed to run from the command line. The script has a corresponding RSpec file that verifies its functionality. The folder structure is:
./main_script.rb
./spec/main_script_spec.rb
Running rspec spec in the top level directory works as expected. Test results from the ./spec/main_script_spec.rb file are shown. I'd like to avoid running this manually every time I change either the main script file or the spec file. All my search results turn up things like guard which (as far as I can tell) are all designed for Rails apps.
How do I setup RSpec to watch for script or spec changes and run automatically with non-Rails Ruby code?
Like David said, Guard can be used to watch a wide variety of files and perform actions when those files are modified. It does not have to be used with a Rails app. I have set up something similar in the past using guard. Here is what I did:
Place the following in your Gemfile:
source 'https://rubygems.org'
gem 'guard'
gem 'guard-shell'
gem 'rspec'
gem 'rb-fsevent', '~> 0.9'
Then run:
$ bundle install
Create a Guardfile in your home directory with:
$ guard init
In the Guardfile, comment out the examples and add this:
guard :shell do
watch(%r{^*\.rb}) { `bundle exec rspec spec/` }
end
This tells guard to watch for modifications to any ruby files in the directory and execute the command bundle exec rspec spec/ when they change (the backticks are used to execute the command in the shell).
Then open up a new terminal window in your current directory and start a guard server to start watching the files:
$ bundle exec guard
Now your Rspec test suite should automatically run when you modify ruby files in the directory.
I used guard at the past, but now I'm using a combination of rspec focus feature and watch command.
It's very simple, just add an f before a describe of it block you want to run the test. So it would becomes fdescribe or fit block. This is the same as adding a tag :focus => true to your block.
We can then filter specs with the focus tag: rspec -t focus
Now, to keeping running theses specs (every 0.5 seconds) with focus tag we call it with watch command:
watch -n 0.5 rspec -t focus
But with that the output won't show colors. So, we need to use with unbuffer.
sudo apt-get install expect
With a little customization:
watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'
Since it's annoying to type this all, I made two alias at my ~/.bash_aliases file (your can use .bashrc as well):
alias focus="watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'"
alias focuss="bundle exec rspec -t focus"
Now I can type focus to keep running it, or for a single focus execution I type focuss
Guard can be used for plain old ruby. I generally have trouble with guard so I like to use watchr, another gem. With a few lines of code you can tell watchr to watch for changes to your files and run a command when they change.
For an example of guard with plain ruby, see the shuhari gem.
update on watchr gem: There appears to be an issue with this gem, perhaps with versions of ruby >= 2.0. The observr gem addresses this issue and works as expected in ruby 2.3.
I have used guard and the guard-rspec addition with great results, and I don't believe it to be Rails-specific. Other Ruby/RSpec projects should work equally well.
The guard documentation recommends the use of Bundler and to "always run Guard through Bundler to avoid errors". I.e. you install it through your Gemfile and always run it with bundle exec guard (or use rubygems-bundler to avoid the bundle exec part).

How to develop a Ruby GEM without having to install it first?

I'm developing a GEM that I've forked and I'm trying to modify it slightly for my app.
I'm finding it difficult and time consuming because for every change I make I have to
uninstall
build
re-install
run the app
Is there an easier way of which doesn't require repeating all steps above?
To use it in some app using bundler
If what you mean is for using it in a app to test it / use it, you can just specify a path for your gem or even point to a git repo in the Gemfile http://gembundler.com/gemfile.html
Like
gem "mygem", :path => "~/code/gems/mygem"
To use it as a standalone gem. i.e: like rspec or rake that can run outside of an app.
Just specify the path to your gem binary when running the gem command, like:
$ ~/path_to_my_gem/bin/mygem some args
If you can execute inside your gem directory (i.e: the command does not create files in the current directory, or needs any specific files from the current directory), just do this:
$ ./bin/mygem some args
Note that this last one is just for future reference, I think it's not applicable in the OP context.
use require_relative to include your files:
require_relative 'yourgem/yourclass'
This is the documentation for the function.

Error running autotest on Windows 7 with Rails 3, Ruby 1.9.2

Installing and running Rails 3 and Ruby 1.9.2 on Windows 7 went rather smooth. It's only now that I want to run autotest that I'm running into problems. The error looks frustratingly simple, but I can't figure out how to solve it.
I have the following gems in my Gemfile:
gem 'autotest'
gem 'autotest-rails-pure'
But then when I run bundle exec autotest, I get:
loading autotest/rails
style: Rails
C:\bin\Ruby192\bin\ruby -I.;lib;test -rubygems -e "['test/unit',
'test/unit/helpers/users_helper_test.rb', 'test/unit/user_test.rb'].each
{ |f| require f }" | C:/bin/Ruby192/lib/ruby/gems/1.9.1/gems/autotest-4.4.1/bin/unit_diff -u
'C:' is not recognized as an internal or external command,
operable program or batch file.
So something seems to go wrong in the formatting of some path, but I'm not sure where and how to solve it. I've tried running this under Cygwin, but to no avail.
I have also tried using the ZenTest gem instead of the autotest and autotest-rails-pure gems and that ran! However, it did not detect any if the test files in the default directories. So that seems to be of not much use either.
Who can help?
Oh, yes! I found the solution! The gems I need in this setup are:
gem 'ZenTest'
gem 'autotest-rails-pure'
The ZenTest gem generates the correct command line string (which actually just has the "unit_diff -u" behind the pipe, without the path). The autotest-rails-pure gem finds the correct test set.
I think this has to do with the way the Windows command shell interprets path names. Normally you can substitute "/" for "\" and it still works, but apparently not when you also use pipes. I'll bet if you can put quotes around the failing command (starting with C:/bin/Ruby192/...) Windows will recognize it as a string.

Resources