I've just switched to Sinatra for a project, and my project is just:
/lib
/test
config.ru
Gemfile
In my config.ru file I have:
require 'rubygems'
require 'bundler'
Bundler.setup unless File.exists?(File.expand_path('../.bundle/environment', __FILE__))
Bundler.require(:default)
Dir.glob File.dirname(__FILE__) + '/lib/*.rb', &method(:require)
run HandHistoryParser
It seems to load, and running rackup starts the server without errors. But I have a simple test script to test the output, and when the script creates HandHistoryParser like
@file_reader = HandHistoryParser::FileReader.new("bulk_hands")
I get this error:
in `<main>': uninitialized constant Object::HandHistoryParser (NameError)
For some reason I'm not able to require the library file, even though it works in irb with both of these requires:
require './lib/hand_history_p开发者_运维技巧arser'
require '../../lib/hand_history_parser
What am I missing?
You error message suggests that this might be a scope problem. As in, the module and class constants are getting defined and so on, but with self set to something other than main/the global scope.
Try changing this:
Dir.glob File.dirname(__FILE__) + '/lib/*.rb', &method(:require)
To:
Dir[__FILE__ + '/../lib/*.rb'] { |file| require file }
wait... what do you mean by test script we're using rack/test right
imho a sinatra app will be much more test-friendly if your main app class can be started independent of config.ru
such that you can require just 'rack/test' and 'app.rb' in your test files
精彩评论