jasmine-ajax/spec/javascripts/webmock-style-spec.js

89 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-02-05 11:32:59 +08:00
describe("Webmock style mocking", function() {
var successSpy, errorSpy, response;
2013-02-19 12:37:17 +08:00
var sendRequest = function() {
2013-02-05 11:32:59 +08:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(arguments) {
if (this.readyState == this.DONE) {
response = this;
}
};
xhr.open("GET", "http://example.com/someApi");
xhr.send();
2013-02-19 12:37:17 +08:00
};
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);
2013-02-05 11:32:59 +08:00
});
it("should push the new stub on the ajaxStubs", function() {
expect(ajaxStubs.length).toEqual(1);
});
it("should set the url in the stub", function() {
expect(ajaxStubs[0].url).toEqual("http://example.com/someApi");
});
it("should set the contentType", function() {
expect(response.responseHeaders['Content-type']).toEqual('application/json');
});
it("should set the responseText", function() {
expect(response.responseText).toEqual('hi!');
});
2013-02-19 12:37:17 +08:00
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');
});
2013-02-05 11:32:59 +08:00
});
describe(".matchStub", function() {
it("should be able to find a stub with an exact match", function() {
var stub = jasmine.Ajax.matchStub("http://example.com/someApi");
expect(stub).toBeDefined();
});
2013-02-20 02:33:40 +08:00
describe("with another stub for the same url", function() {
beforeEach(function() {
jasmine.Ajax.stubRequest("http://example.com/someApi").andReturn({responseText: "no", status: 403});
});
it("should use the latest stub", function() {
var stub = jasmine.Ajax.matchStub("http://example.com/someApi");
expect(stub.status).toEqual(403);
expect(stub.responseText).toEqual('no');
});
});
2013-02-05 11:32:59 +08:00
});
});