Test returning 404, but passing on browser

139 views
Skip to first unread message

Mauricio Klein

unread,
May 18, 2015, 3:54:20 AM5/18/15
to sina...@googlegroups.com
Hello guys!

I'm posting here since I've tried many solutions on internet, without success.

I've written a Sinatra app and everything seems to be working well on browser.

However, when I run my unit tests, they always return status 404

Here are some informations about my code:

[user_controller.rb]
require 'sinatra'
require 'sinatra/base'

# Matches: /users
class UserController < Sinatra::Base
  get '/' do
    "Hello User"
  end
end



[service_controller.rb]
require 'sinatra'
require 'sinatra/base'

# Matches: /services
class ServiceController < Sinatra::Base
  get '/' do
    "Hello Service"
  end
end




$:.unshift(File.dirname(__FILE__))

Dir.glob('./app/{controllers,helpers}/*.rb').each { |file| require file }

#
# Mapping routes
#
map('/users')      { run UserController    }
map('/services')   { run ServiceController }



So, I run the whole environment like this:
/usr/local/bin/foreman start -d .





My test setup:

[test_helper.rb]
ENV['RACK_ENV'] = 'test'
require 'test/unit'
require 'rack/test'


[user_controller_test.rb]
require File.expand_path '../../test_helper.rb', __FILE__
require_relative "../../controllers/user_controller"

class UserControllerTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  def test_user
    get '/'
    assert_equal 200, last_response.status
  end


When I run 'rake', the test above returns 404.


PS: I've simplified the code, since is a lot o things. So, this is in essence my problem.

Maybe there is some error  running tests since I've a config.ru file connectin the whole thing.

Hope you guys can help me.

Thanks in advance! :)

johann.q...@minodes.com

unread,
Feb 10, 2016, 12:20:32 PM2/10/16
to sinatrarb
Hello Mauricio,

I think you need to require your app in the test_helper.rb.

For one of my projects, it looks as follows:

require 'rack/test'
require 'rspec'

ENV
['RACK_ENV'] = 'test'

require File.expand_path '../../app.rb', __FILE__

module RSpecMixin
 include
Rack::Test::Methods
 
def app() App end
end

RSpec.configure { |c| c.include RSpecMixin }


Which is based on http://recipes.sinatrarb.com/p/testing/rspec - i.e. replace "App" in the Mixin with your app's class name.
Reply all
Reply to author
Forward
0 new messages