Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra tests always 404'ing

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?

like image 401
Neil Middleton Avatar asked Jan 19 '23 22:01

Neil Middleton


2 Answers

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.

like image 127
ArneTR Avatar answered Jan 30 '23 15:01

ArneTR


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.

like image 35
Rez B. Avatar answered Jan 30 '23 14:01

Rez B.