Merge pull request #50 from peet/fixPrototype

fix the prototype chain of the FakeXMLHttpRequest object
This commit is contained in:
Gregg Van Hove 2014-04-18 10:50:20 -07:00
commit 56977b188c
3 changed files with 19 additions and 10 deletions

View File

@ -101,7 +101,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.requestHeaders = {}; this.requestHeaders = {};
} }
extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest); extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest());
extend(FakeXMLHttpRequest.prototype, { extend(FakeXMLHttpRequest.prototype, {
open: function() { open: function() {
this.method = arguments[0]; this.method = arguments[0];

View File

@ -2,8 +2,9 @@ describe("FakeXMLHttpRequest", function() {
var xhr; var xhr;
var xhr2; var xhr2;
beforeEach(function() { beforeEach(function() {
var realXMLHttpRequest = jasmine.createSpy('realRequest'), var realXMLHttpRequest = {someOtherProperty: 'someValue'},
fakeGlobal = {XMLHttpRequest: realXMLHttpRequest}, realXMLHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(realXMLHttpRequest),
fakeGlobal = {XMLHttpRequest: realXMLHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal); mockAjax = new MockAjax(fakeGlobal);
mockAjax.install(); mockAjax.install();
xhr = new fakeGlobal.XMLHttpRequest(); xhr = new fakeGlobal.XMLHttpRequest();
@ -118,4 +119,10 @@ describe("FakeXMLHttpRequest", function() {
expect(data['some=thing']).toEqual(['else entirely']); expect(data['some=thing']).toEqual(['else entirely']);
}); });
}); });
describe("when a fake XMLHttpRequest is created", function() {
it("inherits the properties of the real XMLHttpRequest object", function() {
expect(xhr.someOtherProperty).toBe('someValue');
})
})
}); });

View File

@ -7,22 +7,24 @@ describe("withMock", function() {
}; };
it("installs the mock for passed in function, and uninstalls when complete", function() { it("installs the mock for passed in function, and uninstalls when complete", function() {
var xmlHttpRequest = spyOn(window, 'XMLHttpRequest').and.returnValue({open: function() {}, send: function() {}}), var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
fakeGlobal = {XMLHttpRequest: xmlHttpRequest}, xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal); mockAjax = new MockAjax(fakeGlobal);
mockAjax.withMock(function() { mockAjax.withMock(function() {
sendRequest(fakeGlobal); sendRequest(fakeGlobal);
expect(xmlHttpRequest).not.toHaveBeenCalled(); expect(xmlHttpRequest.open).not.toHaveBeenCalled();
}); });
sendRequest(fakeGlobal); sendRequest(fakeGlobal);
expect(xmlHttpRequest).toHaveBeenCalled(); expect(xmlHttpRequest.open).toHaveBeenCalled();
}); });
it("properly uninstalls when the passed in function throws", function() { it("properly uninstalls when the passed in function throws", function() {
var xmlHttpRequest = spyOn(window, 'XMLHttpRequest').and.returnValue({open: function() {}, send: function() {}}), var xmlHttpRequest = jasmine.createSpyObj('XMLHttpRequest', ['open', 'send']),
fakeGlobal = {XMLHttpRequest: xmlHttpRequest}, xmlHttpRequestCtor = spyOn(window, 'XMLHttpRequest').and.returnValue(xmlHttpRequest),
fakeGlobal = {XMLHttpRequest: xmlHttpRequestCtor},
mockAjax = new MockAjax(fakeGlobal); mockAjax = new MockAjax(fakeGlobal);
expect(function() { expect(function() {
@ -32,6 +34,6 @@ describe("withMock", function() {
}).toThrow("error"); }).toThrow("error");
sendRequest(fakeGlobal); sendRequest(fakeGlobal);
expect(xmlHttpRequest).toHaveBeenCalled(); expect(xmlHttpRequest.open).toHaveBeenCalled();
}); });
}); });