Ruby on Rails rspec bug? - ruby

every time i execute rspec, dosen't matter what I have done, if the code is wrong it tells:
nev3rkill#nev3rkill-virtual-machine ~/Documentos/ruby $ rspec
/home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load': no such file to load -- /home/nev3rkill/Documentos/ruby/spec (LoadError)
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `block in load_spec_files'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `map'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in `load_spec_files'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in `run'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in `run_in_process'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in `run'
from /home/nev3rkill/.rvm/gems/ruby-1.9.2-p318/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in `block in autorun'
nev3rkill#nev3rkill-virtual-machine ~/Documentos/ruby $
I have tested with a lot of versions, with ruby 1.9.3,1.9.2 but i don't know. Is it normal?

Unless you're in a path with a spec directory (e.g. the root of your rails project), you also need to tell rspec which spec files to run, either by passing it a directory or file. e.g:
$ rspec path/to/specs/
Will run all the specs in the path/to/specs/ path (recursively), or:
$ rspec spec/your_specs.rb
will run the specs in the spec/your_specs.rb file.

Related

Running a single RSpec spec fails with "`require': cannot load such file"

I am trying to get test-first-ruby-master as instructed but I am getting multiple errors. This is the structure of the files/directory in test-first-ruby-master:
Gemfile Gemfile.lock README.md lib spec
lib contains ruby files and spec contains spec files.
I am trying to execute 00_hello_spec.rb file on my terminal but it doesn't work.
What I have tried
require "lib/00_hello.rb"
require_relative "lib/00_hello.rb"
Errors I get
supritkumars-MacBook-Pro:test-first-ruby-master supritkumarshah$ rspec spec/00_hello_spec.rb
/Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `require': cannot load such file -- lib/00_hello.rb (LoadError)
from /Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Thank you
The require is failing because you're requiring a relative path, 'lib/00_hello.rb', but the directory it's relative to (the root of your project) is not in the list of directories that Ruby is looking in for files to require (the load path, $LOAD_PATH).
There are lots of ways to fix this. Here are some:
Tell rspec to add the root of your project to the load path. Since that's your current directory, you can use .:
rspec -I. spec/00_hello_spec.rb
Change your spec to require_relative '../lib/00_hello'. This works regardless of the current directory or load path — a good idea in command-line programs, although not so important for a learning project.
As B Seven answered, change your spec to require './lib/00_hello'. This requires you to run rspec in the root of your project.
You can even remove the require altogether! When you run rspec, if the current directory has subdirectories named lib and/or spec, rspec adds them to the load path. This also requires you to run rspec in the root of your project. This solution won't work if you need the require when running your program for real (not under rspec).
Note that including the .rb in the filename you required did not cause the problem, but it's not necessary.
Although Dave's answer is correct, I prefer to use:
require './lib/00_hello'
Did you tried adding require 'bundler/setup' at the top of your test helper file?

RSpec throws argument error while using -f formatter

I am using ruby and rspec to write automated test cases along with watir.
C:\Users\san\Dragon>gem list 'rspec'
*** LOCAL GEMS ***
rspec (3.4.0, 2.14.1)
rspec-core (3.4.3, 2.14.8)
rspec-expectations (3.4.0, 2.14.5)
rspec-mocks (3.4.1, 2.14.6)
rspec-support (3.4.1)
When I run the test case using following command.
C:\Users\san\Dragon>rspec -f spec\sampletest_spec.rb:65
I got this error
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/formatters.rb:173:in `find_formatter': Formatter 'spec\sampletest_spec.rb:65' unknown - maybe you meant 'documentation' or 'progress'?. (ArgumentError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-.4.3/lib/rspec/core/formatters.rb:141:in `add'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/configuration.rb:767:in `add_formatter'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/configuration_options.rb:114:in `block in load_formatters_into'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/configuration_options.rb:114:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/configuration_options.rb:114:in `load_formatters_into'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/configuration_options.rb:23:in `configure'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/runner.rb:105:in `setup'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/runner.rb:92:in `run'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/runner.rb:78:in `run'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/lib/rspec/core/runner.rb:45:in `invoke'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.3/exe/rspec:4:in `<top (required)>'
from C:/Ruby193/bin/rspec:23:in `load'
from C:/Ruby193/bin/rspec:23:in `<main>'
Getting same error also for
rspec spec\sampletest_spec.rb --format documentation
rspec --format spec\sampletest_spec.rb
You are messing things up. --format/-f parameter expects the format description itself to be passed to it, while you are passing the name of the test file.
I have no clue, why there are problems with
rspec spec\sampletest_spec.rb --format documentation
this one must work. But anyway, the easiest solution would be to create .rspec file in the project root directory, having the following content:
--format documentation
yes, the only line. As you have this file created, simply run
rspec spec\sampletest_spec.rb
RSpec will read a configuration from this file and apply the desired format.

Failing to run RSpec (uninitialized constant User (NameError)

Trying to run bundle exec rspec spec/models/user_spec.rb but failing to execute (see error below).
Contents of user_spec.rb:
require 'rails_helper'
describe User do
pending "add some examples to (or delete) #{__FILE__}"
end
If I remove the last 3 lines, then it complete with 0 examples and 0 failures. However, when the last 3 lines are present, it generates an error
/spec/models/user_spec.rb:4:in `<top (required)>': uninitialized constant User (NameError)
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:97:in `setup'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:85:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:70:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:38:in `invoke'
from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/exe/rspec:4:in `<top (required)>'
from /usr/local/bin/rspec:23:in `load'
from /usr/local/bin/rspec:23:in `<main>'
It seems it doesn't know what User is and treats it as a constant, when this is infact a model. I have verified in the ruby sandbox that I can create new Users in the database. Any ideas?
Answer:
After removing --warnings from .rspec (thank you kirti), current error was about capybara version being too low (requires 2.2). After installing the latest version (modifying gemfile and bundle install), rspec appears to be executing correctly. Thank you to all for your comments.
I had the same issue, this was due to rspec/rails_helper.rb not being called anywhere.
I added it to the .rspec file, so my file look like this
--color
--require spec_helper
--require rails_helper
This and adding warnings to false in the spec/spec_helper.rb solved it for me !
config.warnings = false
After removing --warnings from .rspec (thank you kirti), current error was about capybara version being too low (requires 2.2). After installing the latest version (modifying gemfile and bundle install), rspec appears to be executing correctly. Thank you to all for your comments.
For me, the issue was that the name of the class was not being correctly called due to it being in another namespace. For example, I had:
RSpec.describe User do
# ... some tests
end
where I should have had
RSpec.describe MyModule::User do
# ... some tests
end
because the user class was defined with the MyModule module.

Why do I get "RAKEFILE ABORTED, NO RAKEFILE FOUND"?

I am attempting to take an interactive Ruby test to no avail.
I downloaded the zip file with no problems and followed the directions for
the terminal command line where I continue to get the following error. I
am using "Ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin10.8.0]".
If anyone can offer any help or suggestions thank you in advance.
INSTRUCTIONS:
Install RSpec
gem install rspec
Enter the course directory. (That's the same directory that this
index.html file is in.)
cd learn_ruby
Enter the 00_hello lab.
cd 00_hello
Open a terminal in this directory
cd 00_hello
This directory is the starting point for this exercise. It contains a
spec file and you'll be adding a ruby file to (eventually) make the
specs pass.
Run the test
rake
Watch it fail
You should see an error. Don't get scared! Try to read it and figure out
what the computer wants to tell you. Somewhere on the first line it
should say something like
no such file to load -- test-first-teaching/hello/hello (LoadError)
I do not get this error message, I get the one below:
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb,
Rakefile.rb)
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in
`<main>'
(See full trace by running task with --trace)
When I run rake --trace I receive:
rake --trace
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb,
Rakefile.rb)
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:632:in
`raw_load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:94:in
`block in load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:165:in
`standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:93:in
`load_rakefile'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:77:in
`block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:165:in
`standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/lib/rake/application.rb:75:in
`run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.1.0/bin/rake:33:in
`<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/rake:19:in `<main>'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in
`<main>'
you have to cd into your project directory before you can run rake routes

Error when trying to run RSPEC. Why?

I can't figure out why my RSPEC isn't working. I reinstalled RSPEC with sudo gem install rspec and then tried again to install with a specific version number (sudo gem install rspec -v 2.11.0)
I continue to get the following error message and I cannot figure out what it means. I believe that I may or may not have version 1 also installed, not sure.
Castillo$ rspec document_spec.rb
/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
require': no such file to load -- document (LoadError) from
/Users/Castillo/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
require' from /Users/Castillo/Desktop/document_spec.rb:1:in <top
(required)>' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:inload' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in block in load_spec_files' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:inmap' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in load_spec_files' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:inrun' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in
run' from
/Users/Castillo/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in
block in autorun'
Add full path to your document.rb file into spec file
or use
require_relative
or
require File.expand_path('../document.rb', __FILE__)

Resources