spyOnProperty respects the allowRespy flag
This commit is contained in:
@@ -137,22 +137,6 @@ describe("SpyRegistry", function() {
|
|||||||
}).toThrowError(/does not have access type/);
|
}).toThrowError(/does not have access type/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("checks if it has already been spied upon", function() {
|
|
||||||
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
|
||||||
subject = {};
|
|
||||||
|
|
||||||
Object.defineProperty(subject, 'spiedProp', {
|
|
||||||
get: function() { return 1; },
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
|
||||||
|
|
||||||
expect(function() {
|
|
||||||
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
|
||||||
}).toThrowError(/has already been spied upon/);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("checks if it can be spied upon", function() {
|
it("checks if it can be spied upon", function() {
|
||||||
var subject = {};
|
var subject = {};
|
||||||
|
|
||||||
@@ -212,6 +196,40 @@ describe("SpyRegistry", function() {
|
|||||||
expect(subject.spiedProperty).toEqual(returnValue);
|
expect(subject.spiedProperty).toEqual(returnValue);
|
||||||
expect(setter).toEqual(spy);
|
expect(setter).toEqual(spy);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("when the property is already spied upon", function() {
|
||||||
|
it("throws an error if respy is not allowed", function() {
|
||||||
|
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
||||||
|
subject = {};
|
||||||
|
|
||||||
|
Object.defineProperty(subject, 'spiedProp', {
|
||||||
|
get: function() { return 1; },
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
||||||
|
}).toThrowError(/spiedProp#get has already been spied upon/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns the original spy if respy is allowed", function() {
|
||||||
|
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
||||||
|
subject = {};
|
||||||
|
|
||||||
|
spyRegistry.allowRespy(true);
|
||||||
|
|
||||||
|
Object.defineProperty(subject, 'spiedProp', {
|
||||||
|
get: function() { return 1; },
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
var originalSpy = spyRegistry.spyOnProperty(subject, 'spiedProp');
|
||||||
|
|
||||||
|
expect(spyRegistry.spyOnProperty(subject, 'spiedProp')).toBe(originalSpy);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#spyOnAllFunctions", function() {
|
describe("#spyOnAllFunctions", function() {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (obj[methodName] && j$.isSpy(obj[methodName]) ) {
|
if (obj[methodName] && j$.isSpy(obj[methodName]) ) {
|
||||||
if ( !!this.respy ){
|
if (this.respy) {
|
||||||
return obj[methodName];
|
return obj[methodName];
|
||||||
}else {
|
}else {
|
||||||
throw new Error(getErrorMsg(methodName + ' has already been spied upon'));
|
throw new Error(getErrorMsg(methodName + ' has already been spied upon'));
|
||||||
@@ -91,8 +91,11 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (j$.isSpy(descriptor[accessType])) {
|
if (j$.isSpy(descriptor[accessType])) {
|
||||||
//TODO?: should this return the current spy? Downside: may cause user confusion about spy state
|
if (this.respy) {
|
||||||
throw new Error(propertyName + ' has already been spied upon');
|
return descriptor[accessType];
|
||||||
|
} else {
|
||||||
|
throw new Error(propertyName + '#' + accessType + ' has already been spied upon');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var originalDescriptor = j$.util.clone(descriptor),
|
var originalDescriptor = j$.util.clone(descriptor),
|
||||||
|
|||||||
Reference in New Issue
Block a user