diff --git a/README.markdown b/README.markdown index f75c195..ff8f97d 100644 --- a/README.markdown +++ b/README.markdown @@ -133,6 +133,9 @@ Putting it all together, you can install the mock, pass some spies as callbacks }); request = mostRecentAjaxRequest(); + expect(request.url).toBe('venues/search'); + expect(request.method').toBe('POST'); + expect(request.data()).toEqual({latLng: ['40.019461, -105.273296']}); }); describe("on success", function() { diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index df4fc36..21bfbb2 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -46,6 +46,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clearAjaxRequests: function() { ajaxRequests = []; + }, + + clearAjaxStubs: function() { + ajaxStubs = []; } } @@ -88,6 +92,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. readyState: 0, + onload: function() { + }, + onreadystatechange: function(isTimeout) { }, @@ -141,6 +148,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.responseHeaders = response.responseHeaders || {"Content-type": response.contentType || "application/json" }; + this.onload(); this.onreadystatechange(); }, @@ -215,7 +223,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }, matchStub: function(url) { - for (var i = 0; i < ajaxStubs.length; i++) { + for (var i = ajaxStubs.length - 1; i >= 0; i--) { var stub = ajaxStubs[i]; if (stub.url == url) { return stub; diff --git a/spec/javascripts/webmock-style-spec.js b/spec/javascripts/webmock-style-spec.js index e6d7a40..e981c32 100644 --- a/spec/javascripts/webmock-style-spec.js +++ b/spec/javascripts/webmock-style-spec.js @@ -1,12 +1,7 @@ describe("Webmock style mocking", function() { var successSpy, errorSpy, response; - beforeEach(function() { - jasmine.Ajax.useMock(); - - jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({reponseText: "hi!"}); - - + var sendRequest = function() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(arguments) { if (this.readyState == this.DONE) { @@ -16,6 +11,23 @@ describe("Webmock style mocking", function() { xhr.open("GET", "http://example.com/someApi"); xhr.send(); + }; + + beforeEach(function() { + jasmine.Ajax.useMock(); + jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({responseText: "hi!"}); + + sendRequest(); + }); + + afterEach(function() { + clearAjaxStubs(); + }); + + it("should allow you to clear all the ajax stubs", function() { + expect(ajaxStubs.length).toEqual(1); + clearAjaxStubs(); + expect(ajaxStubs.length).toEqual(0); }); it("should push the new stub on the ajaxStubs", function() { @@ -31,11 +43,26 @@ describe("Webmock style mocking", function() { }); it("should set the responseText", function() { - console.log(response); expect(response.responseText).toEqual('hi!'); }); - it("should be able to mock url requests", function() { + it("should default the status to 200", function() { + expect(response.status).toEqual(200); + }); + + describe("with another stub for the same url", function() { + beforeEach(function() { + jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({responseText: "no", status: 403}); + sendRequest(); + }); + + it("should set the status", function() { + expect(response.status).toEqual(403); + }); + + it("should allow the latest stub to win", function() { + expect(response.responseText).toEqual('no'); + }); }); describe(".matchStub", function() {