Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Being able to mock a simple HTTParty GET request ^^ (Yay!)

  • Loading branch information...
commit 05278494fa201214cc63a67236584719c40e7f91 1 parent e9ece61
Paul Engel authored
View
2  Gemfile
@@ -12,7 +12,7 @@ end
group :gem_test do
gem "minitest"
- gem "mocha"
+ gem "mocha", :require => "mocha/setup"
gem "pry"
gem "rake"
end
View
9 lib/lock-o-motion.rb
@@ -7,6 +7,8 @@
module LockOMotion
extend self
+ MOCKS_DIR = File.expand_path("../lock-o-motion/mocks", __FILE__)
+
class GemPath
attr_reader :name, :version, :path, :version_numbers
include Comparable
@@ -46,6 +48,13 @@ def skip?(path)
end
end
+ def mock_path(path)
+ path = path.gsub(/\.rb$/, "")
+ if File.exists?("#{MOCKS_DIR}/#{path}.rb")
+ "lock-o-motion/mocks/#{path}"
+ end
+ end
+
end
unless defined?(Lotion)
View
16 lib/lock-o-motion/app.rb
@@ -30,7 +30,7 @@ def dependency(call, path, internal = false)
if File.exists?(absolute_path = "#{load_path}/#{path}.bundle") ||
File.exists?(absolute_path = "#{load_path}/#{path}.rb")
if absolute_path.match(/\.rb$/)
- (@dependencies[call] ||= []) << absolute_path
+ register_dependency call, absolute_path
$:.unshift load_path unless $:.include?(load_path)
else
puts " Warning #{call}\n requires #{absolute_path}".red
@@ -39,11 +39,19 @@ def dependency(call, path, internal = false)
end
end
- puts " Warning Could not resolve dependency \"#{path}\"".red
+ if path.match(/^\//) && File.exists?(path)
+ register_dependency call, path
+ else
+ puts " Warning Could not resolve dependency \"#{path}\"".red
+ end
end
private
+ def register_dependency(call, absolute_path)
+ ((@dependencies[call] ||= []) << absolute_path).uniq!
+ end
+
def setup(&block)
@files = []
@dependencies = {}
@@ -87,6 +95,10 @@ def hook
@hook ||= proc do
def require_with_catch(path, internal = false)
return if LockOMotion.skip?(path)
+ if mock_path = LockOMotion.mock_path(path)
+ path = mock_path
+ internal = false
+ end
if caller[0].match(/^(.*\.rb)\b/)
Thread.current[:lotion_app].dependency $1, path, internal
end
View
23 lib/lock-o-motion/mocks/httparty.rb
@@ -0,0 +1,23 @@
+module HTTParty
+ extend self
+
+ def get(path, options = {}, &block)
+ url = NSURL.URLWithString path.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
+ request = NSURLRequest.requestWithURL url
+ data = NSURLConnection.sendSynchronousRequest request, returningResponse: nil, error: nil
+ NSString.alloc.initWithData data, encoding: NSUTF8StringEncoding
+ end
+
+ def post(path, options = {}, &block)
+
+ end
+
+ def put(path, options = {}, &block)
+
+ end
+
+ def delete(path, options = {}, &block)
+
+ end
+
+end
View
3  lib/motion/lotion.rb
@@ -8,7 +8,7 @@ def require(path, caller)
if absolute_path = resolve(path)
unless (IGNORED_REQUIRES + REQUIRED).include?(absolute_path)
warn [
- "Called `require \"#{path}\"` from #{derive_caller caller}",
+ "Called `require \"#{path}\"` from",
derive_caller(caller),
"Add within Lotion.setup block: ".yellow + "app.require \"#{path}\"".green
].join("\n")
@@ -33,6 +33,7 @@ def warn(*args)
private
def derive_caller(caller)
+ return "<unknown path>" if caller.empty?
file, line = *caller[0].match(/^(.*\.rb):(\d+)/).captures
"#{resolve file}:#{line}"
end
View
14 test/test_helper.rb
@@ -4,4 +4,16 @@
require "minitest/unit"
require "minitest/autorun"
-Bundler.require :gem_default, :gem_test
+Bundler.require :gem_default, :gem_test
+
+def silence(&block)
+ Kernel.module_eval do
+ alias :original_puts :puts
+ def puts(message); end
+ end
+ block.call
+ Kernel.module_eval do
+ alias :puts :original_puts
+ undef :original_puts
+ end
+end
View
15 test/unit/mocks/test_httparty.rb
@@ -0,0 +1,15 @@
+require File.expand_path("../../../test_helper", __FILE__)
+
+module Unit
+ module Mocks
+ class TestHTTParty < MiniTest::Unit::TestCase
+
+ describe "HTTParty" do
+ it "should do something" do
+ # assert something
+ end
+ end
+
+ end
+ end
+end
View
13 test/unit/test_lock-o-motion.rb
@@ -4,8 +4,17 @@ module Unit
class TestLockOMotion < MiniTest::Unit::TestCase
describe LockOMotion do
- it "should work" do
- assert true
+ it "should skip certain files for requirement" do
+ silence do
+ assert_equal true , LockOMotion.skip?("pry")
+ assert_equal true , LockOMotion.skip?("openssl")
+ assert_equal false, LockOMotion.skip?("slot_machine")
+ end
+ end
+
+ it "should mock paths when possible" do
+ assert_equal "lock-o-motion/mocks/httparty", LockOMotion.mock_path("httparty")
+ assert_equal nil, LockOMotion.mock_path("slot_machine")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.