Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 208d1048ba0788e18a623915ede71a440008e41c @KentonWhite committed Jul 6, 2010
Showing with 80 additions and 0 deletions.
  1. +45 −0 Readme.markdown
  2. +35 −0 jasmine_mock.js
45 Readme.markdown
@@ -0,0 +1,45 @@
+# Jasmine Mock #
+
+A simple, lightweight native-Jasmine ajax mocking system
+
+# Installation #
+
+Put jasmine_mock.js in the Jasmine helpers folder
+
+# Usage #
+
+## Register ##
+
+Register the URLs you want to listen to and the JSON response
+
+ mock.register('my/url1/', {status: 200, responseText; 'OK'});
+ mock.register('my/url2/', {status: 404, responseText; 'Error'});
+
+## Run the Code ##
+
+Send a block of code with ajax calls to listen for
+
+ mock.ajax(
+ function() {
+ poll_the_server(args);
+ do_some_more_stuff(args);
+ };
+ );
+
+## Cleanup ##
+
+Remove the url's you are listening on
+
+ mock.clear();
+
+# Caveats #
+
+Jasmine Mock only works with jQuery ajax calls that use $.ajax. The only callback function Jasmine Mock is listening to is the complete function. Make ajax calls like this:
+
+ $.ajax( {
+ type: "GET",
+ url: "my/url",
+ complete: function(transport) {
+ ... stuff to do on complete ...
+ }
+ } )
35 jasmine_mock.js
@@ -0,0 +1,35 @@
+var mock = {}
+
+mock.register = function(matcher, result) {
+ mock.matchers.push(matcher);
+ mock.results.push(result);
+}
+
+mock.ajax = function(block) {
+ spyOn($, 'ajax');
+ block();
+ $.each(
+ $.ajax.argsForCall,
+ function(intIndex, objValue) {
+ ajaxCallArgs = objValue[0];
+ $.each(
+ mock.matchers,
+ function(intMatcherIndex, objMatcher) {
+ if (ajaxCallArgs['url'] == objMatcher) {
+ completeFn = ajaxCallArgs['complete'];
+ completeFn(mock.results[intMatcherIndex]);
+ }
+ }
+ )
+ }
+ );
+}
+
+mock.clear = function() {
+ mock.matchers = [];
+ mock.results = [];
+}
+
+mock.matchers = [];
+
+mock.results = [];

0 comments on commit 208d104

Please sign in to comment.
Something went wrong with that request. Please try again.