Sha256: 7d88480341bcb85ac97cb1c967833b6c49855745ec2c9574c6d479f09b1e89c5

Contents?: true

Size: 1.88 KB

Versions: 24

Compression:

Stored size: 1.88 KB

Contents

require 'rubygems'
gem 'rspec', '~> 1.1.3'
require 'spec'
gem 'mocha', '~> 0.9.0'
require 'mocha'

require 'cgi'
require 'fake_web'
FakeWeb.allow_net_connect = false

module SampleFeeds
  FEED_DIR = File.dirname(__FILE__) + '/feeds/'
  
  def sample_xml(name)
    File.read "#{FEED_DIR}#{name}.xml"
  end
end

module HttpMocks
  def mock_response(type = :success)
    klass = case type
    when :success  then Net::HTTPSuccess
    when :redirect then Net::HTTPRedirection
    when :fail     then Net::HTTPClientError
    else type
    end
    
    klass.new(nil, nil, nil)
  end
  
  def mock_connection(ssl = true)
    connection = mock('HTTP connection')
    connection.stubs(:start)
    connection.stubs(:finish)
    if ssl
      connection.expects(:use_ssl=).with(true)
      connection.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
    end
    connection
  end
end

Spec::Runner.configure do |config|
  config.include SampleFeeds, HttpMocks
  # config.predicate_matchers[:swim] = :can_swim?
  
  config.mock_with :mocha
end

module Mocha
  module ParameterMatchers
    def query_string(entries, partial = false)
      QueryStringMatcher.new(entries, partial)
    end
  end
end

class QueryStringMatcher < Mocha::ParameterMatchers::Base
  
  def initialize(entries, partial)
    @entries = entries
    @partial = partial
  end
  
  def matches?(available_parameters)
    string = available_parameters.shift.split('?').last
    broken = string.split('&').map { |pair| pair.split('=').map { |value| CGI.unescape(value) } }
    hash = Hash[*broken.flatten]
    
    if @partial
      has_entry_matchers = @entries.map do |key, value|
        Mocha::ParameterMatchers::HasEntry.new(key, value)
      end
      Mocha::ParameterMatchers::AllOf.new(*has_entry_matchers).matches?([hash])
    else
      @entries == hash
    end
  end
  
  def mocha_inspect
    "query_string(#{@entries.mocha_inspect})"
  end
  
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
aeden-contacts-0.2.15 spec/spec_helper.rb
aeden-contacts-0.2.16 spec/spec_helper.rb
aeden-contacts-0.2.18 spec/spec_helper.rb
aeden-contacts-0.2.19 spec/spec_helper.rb
aeden-contacts-0.2.20 spec/spec_helper.rb
keavy-contacts-0.2.8 spec/spec_helper.rb
keavy-contacts-0.2.9 spec/spec_helper.rb
rakutenusa-contacts-0.2.13 spec/spec_helper.rb
rakutenusa-contacts-0.2.14 spec/spec_helper.rb
rakutenusa-contacts-0.2.15 spec/spec_helper.rb
rakutenusa-contacts-0.2.16 spec/spec_helper.rb
rakutenusa-contacts-0.2.17 spec/spec_helper.rb
rakutenusa-contacts-0.2.18 spec/spec_helper.rb
rakutenusa-contacts-0.2.19 spec/spec_helper.rb
lperichon-contacts-1.0.8 spec/spec_helper.rb
lperichon-contacts-1.0.7 spec/spec_helper.rb
lperichon-contacts-1.0.6 spec/spec_helper.rb
lperichon-contacts-1.0.5 spec/spec_helper.rb
lperichon-contacts-1.0.4 spec/spec_helper.rb
lperichon-contacts-1.0.3 spec/spec_helper.rb