Strange errors in tests after updating to rspec3 - redirect

After updating a project to Rspec 3, I get the following strange errors in my tests which I am not able to fix:
undefined method `empty?' for 2:Fixnum
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `block in missing_keys'
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `select'
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:251:in `missing_keys'
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:231:in `optimized_helper'
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:220:in `call'
# /opt/local/lib/ruby2.1/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
# ./app/controllers/users_controller.rb:10:in `block in <class:UsersController>'
users_controller line 10 contains a redirect:
redirect_to(user_path(current_user))
In each of these cases, current_user is a mocked User object created using mock_model(User, login: User.make_token) and has the ID value quoted in the error message (before ":Fixnum").
This happens with every redirect that contains an object. Examples:
redirect_to current_user
redirect_to user_path(current_user)
redirect_to url_for([:admin, current_user, :edit])
If I use the object's ID as in redirect_to user_path(current_user.id)), the error does not occur. However, then e.g. url_for breaks as well, since I cannot use url_for([#parent.id, #object.id, :edit]) (the objects cannot be derived from their IDs only but are needed for the path).
This only happens when using mocked objects with RSpec 3 and Rails 4.2.1. Any help would be greatly appreciated since this makes a lot of tests fail right now.

Rspec 3 extracted mocks support into the rspec-activemodel-mocks gem, so you might need to add rspec-activemodel-mocks to your Gemfile in order for the route helpers to behave as expected in your specs.

Related

railstutorial.org gems/notiffany-0.1.1/lib/notiffany/notifier/tmux/client.rb:12:in `version': undefined method

I'm following the Ruby on Rails tutorial at RailsTutorial.org and am on section 3.3.1 (/book/static_pages#cha-static_pages) for running 'rails test' for the first time. I'm using the Cloud9 IDE per the recommendation and as far as I can tell I have followed the script exactly. Everything has matched up to this point.
I have also done some searches on the error and come up with comments about something called tmux but I'm not sufficiently experienced enough in Ruby on Rails or Cloud9 to be sure of what I'm doing. Someone also recommended doing 'bundle exec rake test' as an alternative but I get the same error:
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
/usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/tmux/client.rb:12:in 'version': undefined method '[]' for nil:NilClass (NoMethodError)
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/tmux.rb:69:in '_check_available'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/base.rb:59:in 'initialize'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:100:in 'new'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:100:in '_add'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:62:in 'block (2 levels) in detect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:60:in 'each'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:60:in 'detect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:60:in 'block in detect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:59:in 'each'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier/detected.rb:59:in 'detect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier.rb:180:in '_detect_or_add_notifiers'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier.rb:198:in '_activate'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier.rb:87:in 'initialize'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier.rb:42:in 'new'
from /usr/local/rvm/gems/ruby-2.4.1/gems/notiffany-0.1.1/lib/notiffany/notifier.rb:42:in 'connect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/guard-2.13.0/lib/guard/notifier.rb:11:in 'connect'
from /usr/local/rvm/gems/ruby-2.4.1/gems/guard-2.13.0/lib/guard/notifier.rb:31:in 'notify'
from /usr/local/rvm/gems/ruby-2.4.1/gems/guard-compat-1.2.1/lib/guard/compat/plugin.rb:113:in 'notify'
from /usr/local/rvm/gems/ruby-2.4.1/gems/guard-minitest-2.4.4/lib/guard/minitest/notifier.rb:31:in 'notify'
from /usr/local/rvm/gems/ruby-2.4.1/gems/guard-minitest-2.4.4/lib/guard/minitest/reporter.rb:10:in 'report'
from /usr/local/rvm/gems/ruby-2.4.1/gems/minitest-5.11.1/lib/minitest.rb:791:in 'each'
from /usr/local/rvm/gems/ruby-2.4.1/gems/minitest-5.11.1/lib/minitest.rb:791:in 'report'
from /usr/local/rvm/gems/ruby-2.4.1/gems/minitest-5.11.1/lib/minitest.rb:141:in 'run'
from /usr/local/rvm/gems/ruby-2.4.1/gems/minitest-5.11.1/lib/minitest.rb:63:in 'block in autorun'
I don't see anything called notiffany in the Gemfile, but I've found it's related to Guard. Since I'm using the specific version indicated by the tutorial, I'm not sure if looking for a newer version there is the fix.
It is possible I'm just not searching for the right keywords, so I apologize if this has been answered before. Any help is appreciated.
-Jay
Try this:
sudo yum install -y tmux
As suggested on this Japanese site.

Pass test arguments via my Rake::TestTask . What am I doing wrong?

I have a Rakefile defined with code like so:
namespace :app do |args|
desc "Run App tests from lib/app_tests/test_*.rb"
Rake::TestTask.new do |t,args|
t.name = "testApp"
t.libs << 'lib/pageobjects/app_page_objects'
t.test_files = FileList['lib/app_tests/test_*.rb']
#t.options - passing on command line see below
t.verbose = true
end
...
But, when I run the task, it treats the TESTOPTS args as if they are additional tests that the test runner wants to run. What I want is for each test in t.test_files, pass the 3 arguments to each test. The main error I see is:
cannot load such file -- c:/Users/me/IdeaProjects/impl/util/PROJECT/testclient
Why is Rake treating my TESTOPTS like they are additional items in the t.test_files FileList object? With my experience with JUnit, this seems unusual to me.
And here is the console output:
$ rake app:testapp TESTOPTS="testclient impdev app"
c:/Ruby22-x64/bin/ruby.exe -I"lib;lib/pageobjects/app_page_objects" "c:/Ruby22-x64/lib/ruby/2.2.0/rake/rake_test_loader.rb" "lib/app_tests/test_1.rb" "lib/app_tests/test_2.rb" testclient impdev app
Current dir is : c:/Users/me/IdeaProjects/impl/util/PROJECT
c:/Ruby22-x64/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- c:/Users/me/IdeaProjects/impl/util/PROJECT/testclient (LoadError)
from c:/Ruby22-x64/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from c:/Ruby22-x64/lib/ruby/2.2.0/rake/rake_test_loader.rb:15:in `block in <main>'
from c:/Ruby22-x64/lib/ruby/2.2.0/rake/rake_test_loader.rb:4:in `select'
from c:/Ruby22-x64/lib/ruby/2.2.0/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [ruby -I"lib;lib/pageobjects/app_page_objects" "c:/Ruby22-x64/lib/ruby/2.2.0/rake/rake_test_loader.rb"
"lib/app_tests/test_browse_conditions.rb" "lib/app_tests/test_example.rb" testclient impdev app]
end
EDIT:
I created a test project on GitHub that reproduces the problem. Just download the project and run the tests with 'rake test' :
https://github.com/djangofan/RakeUnitTests/tree/5df4825d105251c81d425d3aef986e3f7d2f9f44
Also, I discovered that parallel_test (without rake) is capable of passing the test args the way that I want to, with a '-o' argument. I just wish I knew how to do it with Rake.

Problems implementing ruby geocoder using Sinatra

According to the ruby geocoder documentation (rubygeocoder.com), it's possible to use the geocoder gem with a Sinatra app, but I'm running into issues getting it to work and haven't been able to find any working examples or related stackoverflow issues either. I think the problem is due to the fact that it's a Sinatra app and not a full rails app.
My Gemfile:
source "https://rubygems.org"
ruby '2.1.2'
gem 'dotenv', '~> 0.10.0'
gem 'pg', '~> 0.17.1'
gem 'rack-flash3'
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "geocoder"
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem "sqlite3"
gem "shotgun"
The model (which has float latitude and longitude columns) that I want to search by:
class Item < ActiveRecord::Base
extend Geocoder::Model::ActiveRecord
attr_accessor :latitude, :longitude
belongs_to :profile
has_and_belongs_to_many :categories
has_many :reports, dependent: :destroy
}
Here's the app.rb code with the '/' route:
class FL < Sinatra::Base
get '/' do
#items = Item.near('Detroit, MI, US')
puts "ITEMS ARE:"
pp #items
erb :index
end
...
end
Here's the relevant app.rb contents:
require 'rubygems'
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/activerecord'
require 'geocoder'
require 'omniauth'
require 'sinatra/flash'
require 'json'
require 'pp'
require 'rack-flash'
require './models/model_init'
require './helpers/helper'
require './auth'
require './admin'
require './api'
class FL < Sinatra::Base
set :root, File.dirname(__FILE__)
enable :logging
enable :sessions
#set :logging, true
register Sinatra::ActiveRecordExtension
register Sinatra::Flash
set :show_exceptions, true if ENV['RACK_ENV'] == 'development'
use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
end
Finally, here is the error i receive:
NoMethodError - undefined method `near' for #<Class:0x0000010750d0b8>:
/Users/bob/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/Users/bob/rails_projects/fl2/api.rb:21:in `block in <class:FL>'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `call'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in `block in compile!'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `[]'
/Users/bob/.rvm/gems/ruby-2.1.2/gems/sinatra-1.4.5/lib/sinatra/base.rb:966:in `block (3 levels) in route!'
Looking at the stacktrace, it looks like its not finding the geocoder package for some reason. I'm not sure if if it's a simple configuration that I'm missing, or if what I'm attempting isn't possible without some gem customization (something beyond my understanding at this point). Any insight, suggestions to try, or examples would be much appreciated. thanks!
Adding my config.ru:
config.ru
require 'bundler/setup'
Bundler.require(:default)
use Bundler.setup(:default) #added this by suggestion
require 'logger'
use Rack::Deflater
Dotenv.load
require "./fl_app"
run FL
I found a solution to this issue, but can't explain why it works. I had to add the line:
reverse_geocoded_by :latitude, :longitude
to my Item model. Once I added that line I was able to use geocoder's 'near' method.
Even though I'm not actually doing any geocoding (converting lat/long to addresses, or vice versa), it still appears to be necessary for the library to work correctly. Thanks #iain for all your helpful advice.

Ruby on Rails Tutorial 3.2 by Michael Hartl Chapter 9. authentication_pages_spec.rb doesn't see sign_in method in utilities.rb

This is my first question on StackOverflow, so bear with me ...
When I use the authentication_pages_spec.rb in Listing 8.6 my tests pass.
Then I define the sign_in method in 'spec/support/utilities.rb' and modify authentication_pages_spec.rb according to Listing 9.5 and, when running
$ bundle exec rspec spec/requests/authentication_pages_spec.rb -e "Authentication"
I get
1) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_2:0x007fc585a87cd8>
# ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels) in <top (required)>'
for all the six tests under "with valid information".
It seems that the spec doesn't see the sign_in function and needs some declaration, anyhow I couldn't find where this is done in the Michael Hartl's code on Github.
Any help is really appreciated.
Just Restart spork and it should work :)
Hartl explains the sign_in helper almost immediately after that in Listing 9.6
I encountered the exact same error messages, based on a subtle mis-placement of an end. Sharing here, in case others run into the same test failures and are seeking a possible solution.
My mistake was in how I authored utilities.rb, which was:
include ApplicationHelper
RSpec::Matchers.define :have_error_message do |message|
match do |page|
page.should have_selector('div.alert.alert-error', text: 'Invalid')
end
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# # Sign in when not using Capybara as well.
# cookies[:remember_token] = user.remember_token
end
end
What the final end needs to be after the RSpec::Matchers block and should read like this:
include ApplicationHelper
RSpec::Matchers.define :have_error_message do |message|
match do |page|
page.should have_selector('div.alert.alert-error', text: 'Invalid')
end
end
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# # Sign in when not using Capybara as well.
# cookies[:remember_token] = user.remember_token
end

Capybara select statement not working with Rspec

Using Capybara with Rails 3, Rspec and Cucumber,
When I use the select statement, such as
select("Unspecified", :from=> 'record_family_')
I get an error, wrong number of arguments (2 for 3). Likewise, if I omit the second argument, I get wrong number of arguments (1 for 3). This happens whether I'm using RSpec or Cucumber. If I switch to Webrat, everything works.
Details: Rails 3.0.4, rspec-core 2.5.1, rspec-rails 2.5.0, capybara 0.1.4.2, rack 1.2.1
The first few lines of the stack trace (with the path simplified) are:
wrong number of arguments (1 for 3) (ArgumentError)
actionpack-3.0.5/lib/action_view/helpers/form_options_helper.rb:131:in `select'
capybara-0.4.1.2/lib/capybara/dsl.rb:104:in `select'
step_definitions/sim_db_steps.rb:412:in `block in <top (required)>'
cucumber-0.10.0/lib/cucumber/core_ext/instance_exec.rb:48:in `instance_exec'
cucumber-0.10.0/lib/cucumber/core_ext/instance_exec.rb:48:in `block in cucumber_instance_exec'
cucumber-0.10.0/lib/cucumber/core_ext/instance_exec.rb:69:in `cucumber_run_with_backtrace_filtering'
cucumber-0.10.0/lib/cucumber/core_ext/instance_exec.rb:36:in `cucumber_instance_exec'
cucumber-0.10.0/lib/cucumber/rb_support/rb_step_definition.rb:62:in `invoke'
Any ideas?
Thanks to Jonas Niklas for pointing out the problem, a name space issue. I had indirectly included ActionView::Helpers::FormOptionsHelper into the test cases so that the ActionView select was conflicting with the Capybara one.
I had a name space issue also with select (it was using kernel instead of page.select). I ended up having to change this:
RSpec.configure do |config|
config.include Capybara::DSL
config.include ActionView::Helpers::FormOptionsHelper
end
To this:
RSpec.configure do |config|
config.include ActionView::Helpers::FormOptionsHelper
config.include Capybara::DSL
end
Just the ordering of the includes fixed it.