Sinatra Extension - 使用Rack :: test进行测试

时间:2012-03-09 10:33:49

标签: 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?
        **...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

我很难掌握应用程序设置def test_cache_dir_available方法失败导致没有这样的方法

任何人都能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

实际上,在同事的帮助下,通过避免使用Test App实现并仅使用Sinatra :: Application

来解决这个问题