Trying to understand the error - ruby

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.

Related

How do I run Rails/Rake from another directory?

Without cd-ing into the root directory of my Rails application, how can I execute a Rails or Rake command for that application.
I tried:
bundle exec rake my_tasks:do_stuff BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile
among other combinations, to no avail.
[Update]
The issue is actually two-fold, bundle doesn't know where the gemfile is and rake doesn't know what to run.
To use Bundler:
BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile bundle exec ...
Note that BUNDLE_GEMFILE has to go before 'bundle exec'.
To use Rake:
rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
To use Rails console:
????
Have yet to figure out how to enter the Rails console from another directory. Looking at the source, I think it may not be possible, because it eventually does File.join('script','rails') to kick off the rails process.
Without you showing the error message you are getting, I'm assuming it has less to do with Bundler than it does Rake. When the rake command is run, it searches for a Rakefile starting in the current directory and traversing up the tree until it finds one. You can override this behavior by explicitly specifying a Rakefile in the options to the rake command. This is done using the -f <RAKEFILE> option.
eg.
bundle exec rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
Bear in mind that your Rake tasks have to be "CWD agnostic". Most tasks and scripts are as they tend to get the project directory based on a path relative to a known file in the directory tree. You probably already understand that, but it's worth mentioning in case the tasks are expecting the current working directory to actually be the rails root. That would be a case where running them from outside the project could potentially be dangerous.

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).

Ruby minitest-ci gem

The documentation for the minitest-ci gem (seemingly the only option for producing test results for a CI tool such as Jenkins) has the extremely annoying habit of not preserving the results of rake minitest:models when invoked as rake minitest - the test results from running minitest:models are deleted prior to running the rest of the tests. minitest-ci's barely-extant documentation claims adding this to test_helper.rb will disable the troublesome auto-clean behavior, but it doesn't:
# Why do SO and GitHub have to use completely different ways of indicating inline code?
# test/helper.rb
MiniTest::Ci.auto_clean = false
Has anyone out there managed to get minitest-ci to preserve all the test result files? I'm reaching wits' end here.
I think ci_reporter gem supports miniTest. This could be another option.

Difficulty installing RSpec on Windows

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.

No colors when using autospec

When I'm using autospec to test a non-Rails Ruby project, I always have trouble getting my tests to show up red or green.
When I run a regular 'spec spec/ --color' command I get normal red/green responses. I've tried putting all sorts of commands in the .autotest file and I can't get that to work.
Also, in my Rails projects, I don't get this problem.
Note:
I do have the ZenTest(4.2.1) and redgreen (1.2.2) gems installed.
I'm currently trying to get it working with this project: http://github.com/coreyhaines/kata-number-to-led
Autospec reads a configuration file at spec/spec.opts for options. Make sure rspec can find this file and that it has the --color option (or --colour for our British friends).

Resources