Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Forked from madrobby/LICENSE.txt
Created May 21, 2011 05:20
Show Gist options
  • Save alexindigo/984280 to your computer and use it in GitHub Desktop.
Save alexindigo/984280 to your computer and use it in GitHub Desktop.
Mock objects

This function creates a mock object from existing JavaScript objects.

var mock = function(a,b,c,d){d='prototype';for(c in a[d])b[d][c]=function(k){return function(){b(k,arguments)}}(c);return b};

// replace XMLHttpRequest with a mock
// the callback function should check if "method" is defined
XMLHttpRequest = mock(
  XMLHttpRequest,
  function(method, parameters){ 
    method && console.log(method, [].slice.call(parameters))
  }
)

// use on a page that has jQuery or Zepto loaded
$.ajax();

// output in console
// open ["GET", "http://zeptojs.com/", true]
// setRequestHeader ["X-Requested-With", "XMLHttpRequest"]
// send [undefined]

// augment with your own implementations

var MockXHR = function(method, parameters){ 
  method && console.log(method, [].slice.call(parameters))
}
XMLHttpRequest = mock(XMLHttpRequest, MockXHR);

MockXHR.prototype.setRequestHeader = function(mehtod, name, value){
  console.log('!!!OMG!!!', name, value);
}

$.ajax();

// open ["GET", "https://gist.github.com/980912", true]
// !!!OMG!!! XMLHttpRequest undefined
// !!!OMG!!! */* undefined
// !!!OMG!!! text/javascript undefined
// send [null]

// on a page that has Prototype.js loaded
new Ajax.Request('/')

// open ["POST", "/", true]
// setRequestHeader ["X-Requested-With", "XMLHttpRequest"]
// setRequestHeader ["X-Prototype-Version", "1.6.1"]
// setRequestHeader ["Accept", "text/javascript, text/html, application/xml, text/xml, */*"]
// setRequestHeader ["Content-type", "application/x-www-form-urlencoded; charset=UTF-8"]
// send [""]
function(
a, // object to mock
b, // callback, gets method name and arguments
c, // placeholder
d // placeholder
) {
d = 'prototype'; // save some bytes by caching "prototype" string
for ( // for each...
c // ...method/property...
in // ...in...
a[d] // the object's prototype
)
b[d] // create a method in the
[c] = function(k) { // prototype of the callback
return // caching the method name in a closure
function () { // the callback will be called with
b(k, arguments) // the method name and arguments
}
}(c); // closure magic
return b // return the callback
}
function(a,b,c,d){d='prototype';for(c in a[d])b[d][c]=function(k){return function(){b(k,arguments)}}(c);return b}
Copyright (c) 2011 Thomas Fuchs, http://mir.aculo.us/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "mock",
"description": "Create mock objects for testing pleasure",
"keywords": [
"testing",
"mock"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment