【问题标题】:How can I use Sinatra to simulate a remote server in RSpec / VCR?如何使用 Sinatra 在 RSpec / VCR 中模拟远程服务器?
【发布时间】:2013-10-23 16:27:05
【问题描述】:

VCR Cucumber 文档展示了许多使用小型 Sinatra 应用程序模拟远程服务器的示例,使用从 vcr_cucumber_helpers.rb 加载的名为 start_sinatra_app 的函数。

我想在我的 Rails / RSpec / VCR 测试中使用类似的东西,但还没有弄清楚如何将 start_sinatra_app(或等效项)放入我的测试框架。我的幼稚方法不起作用,因为——毫不奇怪——它找不到vcr_cucumber_helpers.rb

我需要在以下内容中添加什么才能使其在 RSpec 下工作?还是我在草丛中做错了?

# file: spec/app/models/sinatra_test_spec.rb
require 'spec_helper'

start_sinatra_app(:port => 7777) do
  get("/") { "Hello" }
end

describe "sinatra rspec test" do
  it 'calls the sinatra app' do
    VCR.use_cassette("sinatra_rspec_test") do
      res = Net::HTTP.get_response('localhost', "/", 7777)
      res.body.should == 'Hello'
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails-4 rspec sinatra net-http vcr


    【解决方案1】:

    Here's the code您正在寻找:

    def start_sinatra_app(options, &block)
      raise ArgumentError.new("You must pass a port") unless options[:port]
    
      require 'sinatra'
      require 'support/vcr_localhost_server'
      klass = Class.new(Sinatra::Base)
      klass.disable :protection
      klass.class_eval(&block)
    
      VCR::LocalhostServer.new(klass.new, options[:port])
    end
    

    这又使用VCR::LocalhostServer:

    require 'rack'
    require 'rack/handler/webrick'
    require 'net/http'
    
    # The code for this is inspired by Capybara's server:
    #   http://github.com/jnicklas/capybara/blob/0.3.9/lib/capybara/server.rb
    module VCR
      class LocalhostServer
        READY_MESSAGE = "VCR server ready"
    
        class Identify
          def initialize(app)
            @app = app
          end
    
          def call(env)
            if env["PATH_INFO"] == "/__identify__"
              [200, {}, [VCR::LocalhostServer::READY_MESSAGE]]
            else
              @app.call(env)
            end
          end
        end
    
        attr_reader :port
    
        def initialize(rack_app, port = nil)
          @port = port || find_available_port
          @rack_app = rack_app
          concurrently { boot }
          wait_until(10, "Boot failed.") { booted? }
        end
    
        private
    
        def find_available_port
          server = TCPServer.new('127.0.0.1', 0)
          server.addr[1]
        ensure
          server.close if server
        end
    
        def boot
          # Use WEBrick since it's part of the ruby standard library and is available on all ruby interpreters.
          options = { :Port => port }
          options.merge!(:AccessLog => [], :Logger => WEBrick::BasicLog.new(StringIO.new)) unless ENV['VERBOSE_SERVER']
          Rack::Handler::WEBrick.run(Identify.new(@rack_app), options)
        end
    
        def booted?
          res = ::Net::HTTP.get_response("localhost", '/__identify__', port)
          if res.is_a?(::Net::HTTPSuccess) or res.is_a?(::Net::HTTPRedirection)
            return res.body == READY_MESSAGE
          end
        rescue Errno::ECONNREFUSED, Errno::EBADF
          return false
        end
    
        def concurrently
          # JRuby doesn't support forking.
          # Rubinius does, but there's a weird issue with the booted? check not working,
          # so we're just using a thread for now.
          Thread.new { yield }
        end
    
        def wait_until(timeout, error_message, &block)
          start_time = Time.now
    
          while true
            return if yield
            raise TimeoutError.new(error_message) if (Time.now - start_time) > timeout
            sleep(0.05)
          end
        end
      end
    end
    

    【讨论】:

    • 谢谢! -- 抱歉没早点注意到你的回答!
    【解决方案2】:

    Webmock 做得很好。

    允许连接到本地主机:

    # spec/spec_helper.rb    
    require 'webmock/rspec'
    WebMock.disable_net_connect!(allow_localhost: true)
    

    然后将您的 URL 指向应用程序:

    # spec/spec_helper.rb
    RSpec.configure do |config|
      config.before(:each) do
        stub_request(:any, /api.github.com/).to_rack(SinatraApp)
      end
    end
    

    如需更清晰的示例,请参阅:
    http://robots.thoughtbot.com/how-to-stub-external-services-in-tests

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多