4

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?

Neil Middleton
  • 22,105
  • 18
  • 80
  • 134
  • Do you still get 404 when you replace `@app ||= Sinatra::Application` in your test with: `@app ||= App` ? – scable Aug 17 '11 at 19:15

5 Answers5

7

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
...
  1. Also

Sinatra::Synchrony.patch_tests!

seems to be obsolete.

ArneTR
  • 71
  • 1
  • 1
  • This worked for me. I was using rspec and was able to get away with just returning the appname, without assigning it to an instance variable. – randallreedjr Jan 24 '17 at 16:11
2

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.

Rez B.
  • 21
  • 3
0

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 Learning From the Masters: Sinatra Internals and Rack::Test

barerd
  • 835
  • 3
  • 11
  • 31
0

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.

dwmcc
  • 1,034
  • 8
  • 19
  • The page works in the browser. Is there any other real route config in Sinatra? – Neil Middleton Aug 17 '11 at 15:21
  • Similar question with solutions here: http://stackoverflow.com/questions/6145819/why-am-i-getting-404-errors-with-sinatra-with-passenger-under-nginx – dwmcc Aug 17 '11 at 15:26
0

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
Neil Middleton
  • 22,105
  • 18
  • 80
  • 134
  • Using both the class/modular-style and the non-class-style in the same file/project doesn't seem very 'clean'. Maybe try what I wrote in my other comment! – scable Aug 17 '11 at 19:16