Merge with master

This commit is contained in:
JR Boyens 2013-02-18 20:37:17 -08:00
commit 9b2852195a
3 changed files with 47 additions and 9 deletions

View File

@ -133,6 +133,9 @@ Putting it all together, you can install the mock, pass some spies as callbacks
}); });
request = mostRecentAjaxRequest(); 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() { describe("on success", function() {

View File

@ -46,6 +46,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
clearAjaxRequests: function() { clearAjaxRequests: function() {
ajaxRequests = []; ajaxRequests = [];
},
clearAjaxStubs: function() {
ajaxStubs = [];
} }
} }
@ -88,6 +92,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
readyState: 0, readyState: 0,
onload: function() {
},
onreadystatechange: function(isTimeout) { onreadystatechange: function(isTimeout) {
}, },
@ -141,6 +148,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.responseHeaders = response.responseHeaders || this.responseHeaders = response.responseHeaders ||
{"Content-type": response.contentType || "application/json" }; {"Content-type": response.contentType || "application/json" };
this.onload();
this.onreadystatechange(); this.onreadystatechange();
}, },
@ -215,7 +223,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}, },
matchStub: function(url) { matchStub: function(url) {
for (var i = 0; i < ajaxStubs.length; i++) { for (var i = ajaxStubs.length - 1; i >= 0; i--) {
var stub = ajaxStubs[i]; var stub = ajaxStubs[i];
if (stub.url == url) { if (stub.url == url) {
return stub; return stub;

View File

@ -1,12 +1,7 @@
describe("Webmock style mocking", function() { describe("Webmock style mocking", function() {
var successSpy, errorSpy, response; var successSpy, errorSpy, response;
beforeEach(function() { var sendRequest = function() {
jasmine.Ajax.useMock();
jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({reponseText: "hi!"});
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(arguments) { xhr.onreadystatechange = function(arguments) {
if (this.readyState == this.DONE) { if (this.readyState == this.DONE) {
@ -16,6 +11,23 @@ describe("Webmock style mocking", function() {
xhr.open("GET", "http://example.com/someApi"); xhr.open("GET", "http://example.com/someApi");
xhr.send(); 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() { 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() { it("should set the responseText", function() {
console.log(response);
expect(response.responseText).toEqual('hi!'); 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() { describe(".matchStub", function() {