简体   繁体   中英

Sinatra Extension - testing with Rack::test

I am working on a Sinatra extension which does some settings I would like to test that have happened

The extension code looks like this

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

and the test setup like this

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

I struggle to get hold of the app settings the def test_cache_dir_available method fails cause of no such method

Anyone see what I am doing wrong?

实际上,在同事的帮助下,避免使用TestApp实现,而只使用了Sinatra :: Application解决了此问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM