Sinatra rspec capybara: "rack test requires a rack application" - ruby

When running rspec to test a feature test using Sinatra Base, we are getting the following error.
This is how our feature tests looks
require 'capybara/rspec'
feature 'Enter names' do
scenario 'submitting names' do
visit('/')
fill_in :player_1_name, with: 'Dave'
fill_in :player_2_name, with: 'Mittens'
click_button 'Submit'
expect(page).to have_content 'Dave vs. Mittens'
end
end
This is our Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rspec-sinatra'
gem 'capybara'
error:
1.1) Failure/Error: visit "/"
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/feature/feature_spec.rb:5:in `block (2 levels) in <top (required)>'
1.2) Failure/Error: Unable to find matching line from backtrace
ArgumentError:
rack-test requires a rack application, but none was given
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rack_test/driver.rb:16:in `initialize'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `new'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:372:in `block in <top (required)>'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `call'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:79:in `driver'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/session.rb:103:in `reset!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `block in reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `each'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara.rb:257:in `reset_sessions!'
# /Users/Russell/.rvm/gems/ruby-2.2.3/gems/capybara-2.4.4/lib/capybara/rspec.rb:20:in `block (2 levels) in <top (required)>'
We've tried requiring the spec_helper file in our feature test, and also requiring our app.rb in the feature test and we get a different error saying it cannot load sinatra-base
Here is our spec_helper
ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require 'capybara'
require 'capybara/rspec'
require 'rspec'
Capybara.app = Battle
RSpec.configure do |config|
config.include Capybara::DSL
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Any direction would be massively appreciated.
Thanks
Russ

Have a look here:
http://recipes.sinatrarb.com/p/testing/rspec
It suggests, among other things, adding the following to your spec_helper.rb:
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
end
RSpec.configure do |config|
config.include RSpecMixin
end

Related

Active Record unexpected i18n error

I am building a Ruby project that uses active record but not rails. Inside one of my tests I am trying the following:
it "fails with no driver name" do
command = "Driver"
expect {command_file.process_driver command}.to raise_error(ActiveRecord::RecordInvalid)
end
And here is the method I am trying to call
def process_driver command
driver_name = command.split[1]
Driver.create! :name => driver_name
end
I expect to be passing :name => nil to Driver.create! which should throw a RecordInvalid but instead I get I18n::InvalidLocaleData. Here is the backtrace
expected ActiveRecord::RecordInvalid, got #<I18n::InvalidLocaleData: can not load translations from /Users/me/.rbenv/versions/2.3.1/lib/r...ems/activesupport-5.1.3/lib/active_support/locale/en.yml: expects it to return a hash, but does not> with backtrace:
# ./command_file.rb:81:in `process_driver'
# ./command_file.rb:63:in `block in process'
# ./command_file.rb:51:in `each'
# ./command_file.rb:51:in `each_with_index'
# ./command_file.rb:51:in `process'
# ./spec/command_file_spec.rb:60:in `block (5 levels) in <top (required)>'
# ./spec/command_file_spec.rb:60:in `block (4 levels) in <top (required)>'
# ./spec/spec_helper.rb:75:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:74:in `block (2 levels) in <top (required)>'
And here is my Gemfile
source 'https://rubygems.org'
gem 'sqlite3', '~> 1.3', '>= 1.3.13'
gem 'activerecord', '~> 5.1', '>= 5.1.3'
gem 'pry', '~> 0.10.4'
gem 'rspec', '~> 3.6'
gem 'factory_girl', '~> 4.5'
group :test do
gem 'database_cleaner'
end
I have no locale files of my own.
Any idea what's going on? I am not attempting any kind of translation in this project. I also don't understand why a locale file provided by active_support should fail. I'd be happy to simply disable i18n somehow if that were possible but I don't know how. Any ideas what the problem is?
For what ever reason :en was not set as my default locale. I fixed that in my spec_helper.rb by adding I18n.default_locale = 'en':
I18n.default_locale = 'en' # <--- add this line
RSpec.configure do |config|
# config here...
end
I realize this doesn't fix the larger problem of why the locale file from active_support was not loading, but my challenge was simply to make the error go away, not to use i18n

Nomethod error when trying to make rr mock stubs work

I am setting up a project and have been trying to make rr mocks work but I am getting nomethod error. I have tried moving the required method as well but nothing works. This is my repo
$ rspec spec/views
F
Failures:
1) home/show.html.haml
Failure/Error: stub(view).user { user }
NoMethodError:
undefined method `stub' for #<RSpec::ExampleGroups::HomeShowHtmlHaml:0x007fc245188f08>
# ./spec/views/home/show.html.haml_spec.rb:6:in `block (2 levels) in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
# /Users/saadbinakhlaq/.rvm/gems/ruby-2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
I have tried whatever was mentioned here rr
# Gemfile
group :test do
gem 'rr', require: false
end
# test helper
require File.expand_path('../../config/environment', __FILE__)
require 'your/test/framework' # if you are using something other than MiniTest / Test::Unit
require 'rr'
I got this working finally, I used gem 'rr', '1.1.2' instead of 1.2.0 which got auto installed when running bundler without the gem version.

HTTParty crashing Sinatra

I have a small modular Sinatra app that I'm building to act as a front end for a separate REST API. Everything was going swimmingly until I added HTTParty (to make requests to my API) and now no page will load.
The API server was running on localhost port 9393, but the error persists even when I shut down that server. (And when I comment out the one HTTParty request I have in my routes.
Here's my app.rb file:
ENV["RACK_ENV"] ||= "development"
require 'bundler' require 'sinatra/base' require 'sinatra/contrib/all' require 'json' require 'sinatra/strong-params' require 'haml' require 'sinatra/partial' require 'sass'
# require 'httparty'
require 'pry'
Bundler.setup Bundler.require(:default, ENV["RACK_ENV"].to_sym)
# DATABASE CONFIG
# dbconfig = YAML.load(File.read('./config/database.yml'))
# ActiveRecord::Base.establish_connection dbconfig["#{settings.environment}"]
# Base URL for API calls API_BASE = 'http://localhost:9393'
class PantryApp < Sinatra::Base include HTTParty register Sinatra::Partial
set :root, File.dirname(__FILE__) enable :sessions
# set folder for templates to ../views, but make the path absolute set :views, File.expand_path('../app/views', __FILE__)
# don't enable logging when running tests configure :production, :development do
enable :logging end
# will be used to display 404 error pages not_found do
erb :not_found end
get '/' do
haml :login end
get '/login' do
haml :login end
post '/login' do
# api_token = HTTParty.post(API_BASE + '/token', query: {email: params['email'], password: params['password']})
binding.pry
haml :user_pantry end
# REQUIRE ALL APP FILES Dir["./app/**/*.rb"].each { |f| require f }
end
And when I try to go to /login, I get:
Boot Error
Something went wrong while loading config.ru
Errno::ECONNREFUSED: Connection refused - connect(2) for nil port 80
/Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `initialize' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `open' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:879:in `block in connect' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/timeout.rb:73:in `timeout' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:878:in `connect' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:863:in `do_start' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:852:in `start' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http.rb:1375:in `request' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/httparty-0.13.7/lib/httparty/request.rb:117:in `perform' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/httparty-0.13.7/lib/httparty.rb:545:in `perform_request' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/httparty-0.13.7/lib/httparty.rb:476:in `get' /Users/rileypants/dev/capstone/ui/app.rb:46:in `<class:PantryApp>' /Users/rileypants/dev/capstone/ui/app.rb:26:in `<top (required)>' config.ru:1:in `require' config.ru:1:in `block in inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize' config.ru:1:in `new' config.ru:1:in `inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:113:in `eval' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:113:in `inner_app' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:103:in `assemble_app' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:86:in `proceed_as_child' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:31:in `call!' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/loader.rb:18:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/shotgun-0.9.1/lib/shotgun/favicon.rb:12:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/builder.rb:153:in `call' /users/rileypants/.rvm/gems/ruby-2.2.3#pantry_ui/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' /Users/rileypants/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
and config.ru is simply:
require File.dirname(__FILE__) + '/app.rb'
map('/users') { run UsersController }
map('/') { run PantryApp }
It looks to me like something about the 'get' from my first route in app.rb is leading to httparty instead of Sinatra, but I don't know why or what to do to fix it.
I answered my own question, but I want to leave this here in case it is useful for someone else.
In this case, I needed to not use the include HTTParty in the class, but rather require it at the top. I think using include was what was mixing up the get request.

Upgrading to Rails4 and I cant pass this devise test that passed in Rails3

undefined local variable or method `login_user'
Why are my controllers not getting the extended ControllerMacros ???
Commit in question: https://github.com/shadowbq/cartoque/commit/2fba99c6eac5f9f2f0da2dd464f475cae0bae520
<snip>
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
<...snip>
<snip>
# include devise helpers in controller specs
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
<...snip>
/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError)
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>'
<snip>
module ControllerMacros
def login_admin
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:admin]
#user = FactoryGirl.create(:admin)
sign_in #user
end
end
def login_user
before(:each) do
#request.env["devise.mapping"] = Devise.mappings[:user]
#user = FactoryGirl.create(:user)
sign_in #user
end
end
end
<...snip>
Here is full backtrace as well..
/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError)
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `module_exec'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `subclass'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:82:in `block (2 levels) in expose_example_group_alias_globally'
from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:3:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `load'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `each'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:102:in `setup'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:88:in `run'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in `run'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in `invoke'
from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/exe/rspec:4:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `load'
from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `<main>'
from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'
More file snippets
root#ubuntu:/home/shadowbq/sandbox/cartoque# head -15 spec/controllers/backup_exlusions_controller_spec.rb
require 'spec_helper'
describe BackupExclusionsController do
login_user
before do
#backup_exclusion = BackupExclusion.create
end
it "gets index" do
get :index
assert_response :success
assert_not_nil assigns(:backup_exclusions)
end
https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29
In rspec-rails 3, specs no longer have their type set automatically based on path, so your controller specs no longer have type :controller. You can either:
reneebable this with the config.infer_spec_type_from_file_location! option
explicitly tag your controller specs with type: :controller

unable to create rake DB migration

I am trying to create a migration for a Ruby project using Sinatra. My model class is inside the app.rb file. When I run create_migration:
rake db:create_migration NAME=create_admins
I get the following exception with trace:
rake db:create_migration NAME=create_admins --trace
rake aborted!
Don't know how to build task 'db:create_migration'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task_manager.rb:62:in `[]'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:149:in `invoke_task'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
My app.rb is copied below:
require 'rubygems'
gem 'activerecord'
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))
#ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:host => "localhost",
:database => 'test6.db'
)
class Bill < ActiveRecord::Base
belongs_to :admin
end
class Admin < ActiveRecord::Base
validates :email, presence: true, uniqueness: true
validates :name, presence: true
has_many :bills
end
My Rakefile is copied below:
# Rakefile
require './app'
require 'sinatra'
require 'active_record'
#require 'rake'
Note: I am not using rails - I am using sinatra with activerecord
You can use sinatra-activerecord instead 'activerecord', and make minor changes like below -
in Gemfile
source 'https://rubygems.org'
gem "sinatra"
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
in rakefile.rb
require "./app"
require "sinatra/activerecord/rake"
This should work

Resources