58803

Sinatra tests always 404'ing

Question:

I have a very simple Sinatra app which I'm having trouble testing.

Basically, every single request test returns a 404 when I know from testing in the browser that the request works fine. Any ideas as to what the problem might be?

test_helper.rb:

ENV["RACK_ENV"] = 'test' $: << File.expand_path(File.dirname(__FILE__) + '/../lib') require 'app' Sinatra::Synchrony.patch_tests! class Test::Unit::TestCase include Rack::Test::Methods end

app_test.rb

require 'test_helper' class AppTest < Test::Unit::TestCase def app @app ||= Sinatra::Application end def test_it_says_hello get "/" assert_equal 200, last_response.status end end

app.rb

$: << 'config' require "rubygems" require "bundler" ENV["RACK_ENV"] ||= "development" Bundler.require(:default, ENV["RACK_ENV"].to_sym) require ENV["RACK_ENV"] class App < Sinatra::Base register Sinatra::Synchrony get '/' do status 200 'hello, I\'m bat shit crazy and ready to rock' end end

Gemfile

source :rubygems gem 'daemons' gem 'sinatra' gem 'sinatra-synchrony', :require => 'sinatra/synchrony' gem 'resque' gem 'thin' group :test do gem 'rack-test', :require => "rack/test" gem 'test-unit', :require => "test/unit" end

Why can I not get this normally very simple thing working?

Answer1:

I had quite the same problem with only HTTP-404 coming in return.

I solved it with giving another return in the "app" function.

class IndexClassTest < Test::Unit::TestCase def app @app = Foxydeal #appname NOT Sinatra::Application end ... <ol><li>Also </li> </ol>

Sinatra::Synchrony.patch_tests!

seems to be obsolete.

Answer2:

Under your app_test.rb do this instead of what you have now:

def app @app ||= App.new end

This will work with your your class style like you had it in the beginning, no need to switch to the non-class/modular style.

Answer3:

It may seem logical, but are your routes configured correctly? If a route isn't correctly configured, it'll throw 404 errors left and right.

Answer4:

Figured it out.

app.rb

$: << 'config' require "rubygems" require "bundler" ENV["RACK_ENV"] ||= "development" Bundler.require(:default, ENV["RACK_ENV"].to_sym) require ENV["RACK_ENV"] class App < Sinatra::Base register Sinatra::Synchrony end get '/' do status 200 'hello, I\'m bat shit crazy and ready to rock' end

Answer5:

You may simply do this:

class AppTest < Test::Unit::TestCase def app Sinatra::Application end

You can get a solid understanding of sinatra tests by reading <a href="http://patshaughnessy.net/2012/3/7/learning-from-the-masters-sinatra-internals" rel="nofollow">Learning From the Masters: Sinatra Internals</a> and <a href="https://github.com/brynary/rack-test" rel="nofollow">Rack::Test</a>

Recommend

  • Rake NoMethodError: undefined method 'scan' for Lexicon:Class
  • Add routes to Dashing dashboard
  • NoMethodError: undefined method `assert_equal' with minitest
  • “com.android.vending.INSTALL_REFERRER” somehow parameters are lost and changed to (not%20set)
  • Ruby Koans: Where are the quotes in this return value?
  • Issue with rufus-scheduler when we have multiple EC2 instances
  • Rails and RSpec: Testing controllers with the same name in different namespace (module)
  • Perl Dbi and stored procedures
  • How to test SessionsController for OmniAuth with Minitest
  • rails 5 carrierwave no route matches for image
  • Main Thread Checker: UI API called on a background thread iOS 11 Xcode 9 GM Seed
  • Outlook to Excel hyperlink issue
  • How to resolve permission denied maybe missing internet permission?
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Django foreign key drop down
  • HttpURLConnection.getOutputStream() takes 20 seconds. Why?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • VSCode change debug shell to bash on windows
  • Getting unused unique values on a SQL table
  • How to handle elastic beanstalk deployment so it uploads only changed files
  • AppleScript : find open tab in safari by name and open it
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Build Successful but not running on simulator
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • I18n locale disregarding fallbacks
  • Spring Cloud Microservice Architecture Confusion
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Connect .sks to skscene.h
  • Grails calculated field in SQL
  • How do I access an unhandled exception in an MVC Error view?
  • iOS: Detect app start via notification press
  • Spring Data JPA custom method causing PropertyReferenceException
  • C# - Serializing and deserializing static member
  • How to add date and time under each post in guestbook in google app engine
  • Sending data from AppleScript to FileMaker records
  • JSON with duplicate key names losing information when parsed
  • Return words with double consecutive letters
  • How to include full .NET prerequisite for Wix Burn installer
  • Is it possible to post an object from jquery to bottle.py?