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).
Related
Rails 6.1.3.1
Rspec
basic behavior spec code:
describe "index" do
it "should show me the list" do
visit dashboard_targets_path
end
end
the routes file
namespace :dashboard do
resources :targets
end
error shows me the exception, but strangely it appears as if it isn't calling through to the app, just fails right in my test code:
1) interaction for Dashboard::TargetsController index should show me the list
Failure/Error: visit dashboard_targets_path
NoMethodError:
undefined method `empty?' for nil:NilClass
# ./spec/system/dashboard/targets_behavior_spec.rb:16:in `block (3 levels) in <top (required)>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/webmock-3.12.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <main>'
# /Users/jason/.rvm/gems/ruby-2.6.6/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <main>'
it seems to be failing inside the test code, if I drop into the debugger there and run dashboard_targets_path directly I also get the same exception, so the problem is just using the helper within the TEST ENVIRONMENT
within the dev environment, this function works
the problem here was the the config/environments/test.rb file does have default_url_options.
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
when you hit errors that disappear into the Rails gems, a good way to debug them is like so:
begin
// your failing code here
rescue StandardError => e
puts e.backtrace
byebug
puts e.inspect
raise (e)
end
WARNING: do not leave this code in your app or check it in unless you explicitly want to use exception handling for flow control (not recommended!). This is recommended ONLY for debugging purposes.
here you will see the full backtrace to the line number in the Gem where it is failing. when debugging Gems be careful— remember to un-do any changes you make and know that your monkey-patching inside of the Gem code doesn't affect your production code.
I am writing up a rspec test - and for some reason, i am told that the method any_instance is undefined. I am quite surprised, because I have a very similar expectations in one of my controllers rspec files - and it works fine. Any ideas why this could be happening?
require 'spec_helper'
describe Subscriber do
it {should belong_to :user}
describe "send_message should use mobile to send message" do
subscriber = Subscriber.new(:number => "123")
Mobile.any_instance.should_receive(:send_sms).with("123")
subscriber.send_message("hello!")
end
end
Error
/subscriber_spec.rb:9:in `block (2 levels) in <top (required)>':
undefined method `any_instance' for Mobile:Class (NoMethodError)
My rspec version (taken from my gemfile is)
gem "rspec-rails", ">= 2.11.0", :group => [:development, :test]
Thanks!
Really clear: you didn't wrap your test in an it block. That's all.
I'm upgrading a Rails 2.3.5 app to Rails 3.0.3. But my integration tests
aren't working. I'm getting this error:
NoMethodError: undefined method `content_type' for nil:NilClass
The line to blame is
assert_select "input#artist.title.unsolved", 1
My test_helper.rb for webrat looks likes this:
require "webrat"
require 'webrat/core/matchers'
include Webrat::Methods
Webrat.configure do |config|
config.mode = :rack
end
I'm using shoulda 2.11.3 and webrat 0.7.3 for the testing. I've read,
that webrat and shoulda are compatible with Rails3.
Anyone an idea how to fix this?
Thanks!
tux
ADDITION:
It seems, that the NoMethodError appears from shoulda, not from Webrat, as mentioned in the title. Here is the trace:
NoMethodError: undefined method `content_type' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/actionpack-3.0.3/lib/action_dispatch/testing/assertions/selector.rb:605:in `response_from_page_or_rjs'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/actionpack-3.0.3/lib/action_dispatch/testing/assertions/selector.rb:213:in `assert_select'
test/integration/user_integration_test.rb:52:in `block (3 levels) in <class:UserIntegrationTest>'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:412:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:412:in `block in run_current_setup_blocks'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:411:in `each'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:411:in `run_current_setup_blocks'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:393:in `block in create_test_from_should_hash'
And here is the whole block context around the assert_select:
class SongIntegrationTest < ActionController::IntegrationTest
context "a visitor" do
context "solving a song" do
setup do
#song = Song.make
visit song_path(#song)
end
should "have two guess fields" do
assert_select "input#artist.title.unsolved", 1
assert_select "input#title.title.unsolved", 1
end
Maybe the assert_select isn't longer available in Rails 3 with Shoulda.
Hope someone can help me!
thx!
There is a fork of webrat that works with rails3 at https://github.com/joakimk/webrat. At some point it should get pulled into the main branch. rails 2.3 is still very popular at this time.
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.
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.