From 337df30b51d2c1bdddcb7fbd05f0ccf46a7a31b3 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 2 Jul 2012 17:39:56 +0200 Subject: expectRequest and respond{JSON,XML} functions to simplify the tests --- javascript/spec/specHelper.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'javascript/spec/specHelper.js') diff --git a/javascript/spec/specHelper.js b/javascript/spec/specHelper.js index 747fa9d..21a0cb7 100644 --- a/javascript/spec/specHelper.js +++ b/javascript/spec/specHelper.js @@ -1,31 +1,40 @@ var specHelper = (function() { // HELPERS - function respondXML(request, content) { + function setupFakeXHR() { + this.xhr = sinon.useFakeXMLHttpRequest(); + var requests = this.requests = []; + this.xhr.onCreate = function (xhr) { + requests.push(xhr); + }; + this.expectRequest = expectRequest; + this.respondJSON = respondJSON; + this.respondXML = respondXML; + } + + function expectRequest(url, content) { + expect(this.requests.length).toBe(1); + expect(this.requests[0].url).toBe(url); + expect(this.requests[0].requestBody).toBe(content); + } + + function respondXML(content) { + var request = this.requests.pop(); header = { "Content-Type": "application/xml;charset=utf-8" }; body = '\n'; body += content; request.respond(200, header, body); } - function respondJSON(request, object) { + function respondJSON(object) { + var request = this.requests.pop(); header = { "Content-Type": "application/json;charset=utf-8" }; body = JSON.stringify(object); request.respond(200, header, body); } - function setupFakeXHR() { - this.xhr = sinon.useFakeXMLHttpRequest(); - var requests = this.requests = []; - this.xhr.onCreate = function (xhr) { - requests.push(xhr); - }; - } - return { - respondJSON: respondJSON, - respondXML: respondXML, - setupFakeXHR: setupFakeXHR + setupFakeXHR: setupFakeXHR, } })(); -- cgit v1.2.3