Sinatra tests always 404'ing

不想你离开。 提交于 2020-01-04 02:54:34

问题


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?


回答1:


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.




回答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.




回答3:


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.




回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/7095217/sinatra-tests-always-404ing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!