Ruby Sinatra扩展-使用机架进行测试::测试
我正在开发一个Sinatra扩展,它有一些我想测试的设置 扩展代码如下所示Ruby Sinatra扩展-使用机架进行测试::测试,ruby,sinatra,Ruby,Sinatra,我正在开发一个Sinatra扩展,它有一些我想测试的设置 扩展代码如下所示 module Sinatra module Cache # Create a cache module Helpers def cache! **...implementation...** end #check if cached and load from the cache def cached? **...i
module Sinatra
module Cache
# Create a cache
module Helpers
def cache!
**...implementation...**
end
#check if cached and load from the cache
def cached?
**...implementation...**
end
#set some default values at startup - inject some app settings
def self.registered(app)
app.helpers Sinatra::Cache::Helpers
app.set :cache_dir , "/tmp"
end
end
end
register Cache
end
class TestApp < Sinatra::Base
register Sinatra::Cache
configure do
#set :cache_dir, YAML.load_file(File.expand_path("cache.yml", File.dirname(__FILE__)))
end
get '/' do
end
end
class Helper
include Sinatra::Cache::Helpers
end
class SinatraExtTest < Test::Unit::TestCase
include Rack::Test::Methods
attr_accessor :helper
def app
TestApp.new
end
def setup
Sinatra::Base.set :environment, :test
@helper = Helper.new
end
def test_TestApp_loaded
get '/'
assert last_response, "no response"
end
def test_ext_available
assert @helper.methods.include?(:cache!)
end
def test_cache_dir_available
get '/'
assert app.cache_dir
end
end
测试设置是这样的
module Sinatra
module Cache
# Create a cache
module Helpers
def cache!
**...implementation...**
end
#check if cached and load from the cache
def cached?
**...implementation...**
end
#set some default values at startup - inject some app settings
def self.registered(app)
app.helpers Sinatra::Cache::Helpers
app.set :cache_dir , "/tmp"
end
end
end
register Cache
end
class TestApp < Sinatra::Base
register Sinatra::Cache
configure do
#set :cache_dir, YAML.load_file(File.expand_path("cache.yml", File.dirname(__FILE__)))
end
get '/' do
end
end
class Helper
include Sinatra::Cache::Helpers
end
class SinatraExtTest < Test::Unit::TestCase
include Rack::Test::Methods
attr_accessor :helper
def app
TestApp.new
end
def setup
Sinatra::Base.set :environment, :test
@helper = Helper.new
end
def test_TestApp_loaded
get '/'
assert last_response, "no response"
end
def test_ext_available
assert @helper.methods.include?(:cache!)
end
def test_cache_dir_available
get '/'
assert app.cache_dir
end
end
类TestApp有人知道我做错了什么吗?在同事的帮助下,通过避免使用TestApp实现而只使用Sinatra::Application,实际解决了这个问题