ruby 黄瓜ActiveResource与Webmock伪造

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 黄瓜ActiveResource与Webmock伪造相关的知识,希望对你有一定的参考价值。

require 'fakeweb'
require 'fake_resource'
ActiveResource::Base.send :include, ActiveResource::FakeResource

Before do
  FakeWeb.allow_net_connect = false
end

After do |scenario|
  ActiveResource::FakeResource.clean
end
require 'FakeWeb'

module ActiveResource

  #
  # The FakeResource fakes ActiveResources so that no real resources are transferred. It catches the creation methods
  # and stores the resources internally instead of sending to a remote service and responds these resources back on
  # request.
  #
  # Additionally it adds a save! method and can be used in conjunction with Cucumber/Pickle/FactoryGirl to fully
  # fake a back end service in the BDD cycle
  #
  module FakeResource

    @@fake_resources = []

    def self.clean
      FakeWeb.clean_registry
      @@fake_resources = []
    end

    def self.included(base)
      base.class_eval do

        def save
          @@fake_resources << self
          update_fake_responses
        end

        def destroy
          @@fake_resources.delete(self)
          update_fake_responses
        end

        def self.delete(id, options = {})
          puts "delete"
          @@fake_resources.delete_if {|r| r.id == id }
          #update_fake_responses
        end

        def self.exists?(id, options = {})
          not @@fake_resources.select {|r| r.id == id}.blank? 
        end

      end
    end

    def save!
      save
    end

    private

      def update_fake_responses
        FakeWeb.clean_registry

        @@fake_resources.each do |r|
          FakeWeb.register_uri(:get, element_uri, :body => r.to_xml)
        end

        FakeWeb.register_uri(:get, collection_uri, :body => @@fake_resources.to_xml)
      end

      def element_uri
        "#{base_uri}#{element_path}"
      end

      def collection_uri
        "#{base_uri}#{collection_path}"
      end

      def base_uri
        "#{connection.site.scheme}://#{connection.user}:#{connection.password}@#{connection.site.host}:#{connection.site.port}"
      end

    end
end

以上是关于ruby 黄瓜ActiveResource与Webmock伪造的主要内容,如果未能解决你的问题,请参考以下文章

ruby 在ActiveResource中使用Auth标记

黄瓜ruby报告错误“无法修改冻结的String(RunTimeError)”

Node.js BDD。黄瓜,小黄瓜还是黄瓜? [关闭]

ruby 缺少黄瓜的翻译检查。

ruby 缺少黄瓜的翻译检查。

Ruby on Rails。捆绑器。黄瓜。耙中止!命令失败,状态为 (1)