I'm trying to put some specs around a new rails 3 project I am working on, and my first test doesn't seem to be able to find a model.
I've installed rspec from the command line using:
sudo gem install rspec --pre
and then I put the following in my Gemfile
gem "rspec-rails", ">= 2.0.0.beta.1"
But when I run my test I get
./spec/models/world_spec.rb:1: uninitialized constant World (NameError)
rake aborted!
Command /opt/local/bin/ruby -Ilib -Ispec "./spec/models/world_spec.rb" failed
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:71:in 'define'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1112:in 'verbose'
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:57:in 'send'
/opt/local/lib/ruby/gems/1.8/gems/rspec-core-2.0.0.beta.4/lib/rspec/core/rake_task.rb:57:in 'define'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 'call'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 'execute'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 'each'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 'execute'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in 'invoke_with_call_chain'
/opt/local/lib/ruby/1.8/monitor.rb:242:in 'synchronize'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in 'invoke_with_call_chain'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in 'invoke'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in 'invoke_task'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'each'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in 'top_level'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in 'run'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in 'standard_exception_handling'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in 'run'
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/opt/local/bin/rake:19:in 'load'
/opt/local/bin/rake:19
My spec is in spec/models/world_spec.rb, and looks like
describe World, "#hello" do
it "should be invalid" do
World.new.should be_invalid?
end
end
I tried adding a line like require "app/model/world" and require "world" but to no success.
Does anyone know what I'm doing wrong?
Seems in rails 3 I need to require 'spec_helper'
require 'spec_helper'
describe World do
it "should be invalid" do
World.new.should be_invalid?
end
end
But that might just be because I'm running it from rake spec if you're using watchr or some other mechanism you might be able to get that done for you.
Related
When I run rspec test I get:
/home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:2:in `block in ': uninitialized constant Features (NameError)
I suppose problem is only on my local machine because this is repo from trust source (another users haven't got this problem). The repo I'm talking about: https://github.com/netguru-training/katowice-ror-workshops-2015
I read on Stack that the problem could be missing line:
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
But I've got it.
Where could be a problem?
And this is a whole log from console:
jasiek#jasiek-HP-EliteBook-8470p:~/Desktop/katowice-ror-workshops-2015$ RAILS_ENV=test bundle exec rspec
/home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:2:in block in <top (required)>': uninitialized constant Features (NameError)
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core.rb:97:inconfigure'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/support/features.rb:1:in <top (required)>'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:inblock in '
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:in each'
from /home/jasiek/Desktop/katowice-ror-workshops-2015/spec/rails_helper.rb:23:in'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:in require'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:inblock in requires='
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:in each'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration.rb:1280:inrequires='
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:109:in block in process_options_into'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:108:ineach'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:108:in process_options_into'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/configuration_options.rb:21:inconfigure'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:101:in setup'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:88:inrun'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:73:in run'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/lib/rspec/core/runner.rb:41:ininvoke'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/gems/rspec-core-3.3.0/exe/rspec:4:in <top (required)>'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/rspec:23:inload'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/rspec:23:in <main>'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/ruby_executable_hooks:15:ineval'
from /home/jasiek/.rvm/gems/ruby-2.2.0-preview1/bin/ruby_executable_hooks:15:in `'
I know its late.
But for other people who may come here for searching the answer. I was facing the same issue.
So when I checked rails_helper.rb file inside spec I found that this line
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
was commented initially so I un-commented it and everything worked fine.
I am using
gem 'capybara', '~> 2.15', '>= 2.15.4'
gem 'rspec-rails', '~> 3.6'
Try adding the following in your spec/support/features.rb file:
require '../../spec/support/features/session_helpers'
So, it becomes:
require '../../spec/support/features/session_helpers'
RSpec.configure do |config|
config.include Features::SessionHelpers, type: :feature
end
I have a problem with a very basic Rspec code, the same problem as the question 'undefined method `get' for #'.
But in my case none of the solutions given have worked for me!
I have my Rspec code at '/RailsProject/spec/controllers' and the code is:
require "../spec_helper"
describe "ApiMobile", :type => :controller do
it "Log In" do
get 'apiMobile/v0/logIn/test'
expect(response).to be_success
end
end
As you can see I've followed all the instructions but I still have the problem:
1) ApiMobile Log In
Failure/Error: get 'apiMobile/v0/logIn/test'
NoMethodError:
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x000000023b5ec8>
# ./api_mobile_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.00056 seconds
1 example, 1 failure
I have missed something or similar?
Thanks!
I finally discovered what was happening: Peter Alfvin was right, the fault was the isntallation.
I added at the Gemfile 'gem rspec' and at the command line typed 'rspec --init' after the 'bundle install'. But I also needed to add the gem 'rspec-rails' and type 'rails generate rspec:install'.
Now the get command works (I have another error but I think that is related to routes).
I'm trying to create an xml runner to make a result report of Rails unit testing. Here is a code I have:
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class FastFailRunner < Test::Unit::UI::Console::TestRunner
def add_fault(fault)
#faults << fault
nl
output("%3d) %s" % [#faults.length, fault.long_display])
output("--")
#already_outputted = true
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
nl
output(#result)
end
end
Test::Unit::AutoRunner::RUNNERS[:fastfail] = proc do |r|
FastFailRunner
end
When I run it as TESTOPTS="/home/alex/RubymineProjects/app2/test/unit/runner.rb --runner=xml" rake test
... I get an error (pretty weird error)
/home/alex/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
gem install minitest
/home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
Errors running test:units! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
I tried to require test/unit/ui/xml/testrunner.rb but no luck.
Any thoughts?
If you are in Ruby 1.8.7 you can say:
require 'test/unit/ui/console/testrunner'
p Test::Unit::UI::Console::TestRunner # => no problem
(There is no such file as test/unit/ui/xml/testrunner.rb so I'm not sure what you were up to there.)
Look in the docs in test/unit.rb, there's actually sample code showing you how to do this require: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html.
However, the problem is that you are in Ruby 1.9.3. There is no /test/unit in Ruby 1.9.3! Well, there is, but it's just a compatibility layer for basic tests; there is certainly no test/unit/ui/console/testrunner, and no module/class Test::Unit::UI::Console::TestRunner.
Instead, there's minitest. You can read the docs on minitest to see how to make a test runner. http://docs.seattlerb.org/minitest/
One thing to consider is that Test::Unit was included in the default Ruby 1.8.7 installation. If you want to use it in with a later version of Ruby then go ahead and install it as a gem.
sudo gem install test-unit
http://test-unit.rubyforge.org/
I'm trying to get Facebook logins working on my app using Rails, Authlogic, and Omniauth. I'm following the first steps of http://railscasts.com/episodes/235-omniauth-part-1 as precisely as possible.
I have:
added the omniauth gem, run bundle install.
created a new Authentication nifty-scaffold with fields user_id, provider, and uid.
added the route: match '/auth/:provider/callback' => 'authentications#create'
added to config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '20...my_app_id', 'bc...my_app_secret'
end
The server then fails to restart:
/Users/san/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:
in 'const_missing_from_s3_library': uninitialized constant
OmniAuth::Strategies::Facebook (NameError)
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in 'const_missing'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'const_get'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'provider'
from /Users/san/Documents/sanj/myapp/config/initializers/omniauth.rb:2:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:7:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'new'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'block in build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'inject'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application/finisher.rb:35:in 'block in <module:Finisher>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'instance_exec'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'run'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:50:in 'block in run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:134:in 'initialize!'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:77:in 'method_missing'
from /Users/san/Documents/sanj/myapp/config/environment.rb:5:in '<top (required)>'
from /Users/san/Documents/sanj/myapp/config.ru:3:in 'block in <main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/Documents/sanj/myapp/config.ru:1:in 'new'
from /Users/san/Documents/sanj/myapp/config.ru:1:in '<main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'parse_file'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:253:in 'wrapped_app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:204:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:30:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in 'tap'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in '<top (required)>'
from script/rails:7:in 'require'
from script/rails:7:in '<main>'
I had to do 'require "omniauth"' or 'require "omniauth-facebook"' in my config/development.rb file.
The omniauth gem was just bumped to version 1.0 as of yesterday (http://intridea.com/2011/11/2/omniauth-1-0), and there are some changes to how the gem is used that are not covered in the railscast.
I found my solution on this page: https://github.com/intridea/omniauth/wiki/OmniAuth-1.0
The biggest different is that each strategy is now contained within its own gem, so for facebook, you would change the line in your gem file that currently reads "gem 'omniauth'" to "gem 'omniauth-facebook'".
I didn't have to change any other code, and you may have to run "bundle update" to get everything to click through.
I just ran into this problem and this is what was wrong with me:
You can't have your facebook oauth gem in the Development list in your gem file. Heroku only reads the Production ones, so move it up out of the Development list.
I updated my GEMFile with:
group :development, :test do
gem 'rspec'
gem 'webrat'
gem 'rspec-rails'
end
And ran bundle install.
Now I already have a HomeController, so I manually created this:
/spec/controllers/home_controller_spec.rb
I don't have an about page so I started my test off with:
require 'spec_helper'
describe HomeController do
describe "Get 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
end
end
Now I did:
rspec spec/
Do I need to update some other files for rspec to work, don't understand this error message.
UPDATE
I changed the if to it, now I'm getting:
file to load -- spec_helper (LoadError)
from /Users/someuser/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /Users/someuser/dev/rscs/example.com/spec/controllers/home_controller_spec.rb:1
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load_spec_files'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `map'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/configuration.rb:327:in `load_spec_files'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/command_line.rb:18:in `run'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:55:in `run_in_process'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:46:in `run'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/gems/rspec-core-2.2.1/lib/rspec/core/runner.rb:10:in `autorun'
from /Users/someuser/.rvm/gems/ruby-1.8.7-p302#rails3/bin/rspec:19
See the top line of the backtrace.
/Users/someuser/dev/rscs/example.com/spec/controllers/home_controller_spec.rb:6: syntax error, unexpected kDO, expecting kTHEN or ':' or '\n' or ';' (SyntaxError)
That error message points to a syntax error in your spec file—namely, on line 6, there is a do where it was not expected. That's where you should check first.
if "should be successful" do
I'm betting you meant that if to be an it ;) it is the RSpec method for defining a particular aspect of the class to test, and it, unlike the if operator, can take a block.