Allow callThrough to call constructor functions without errors

This commit is contained in:
Elliot Nelson
2020-01-28 19:40:44 -05:00
parent 5e98ee951c
commit 9febe3159d
3 changed files with 56 additions and 30 deletions

View File

@@ -808,6 +808,25 @@ describe("Env integration", function() {
expect(originalFunctionWasCalled).toEqual(true);
});
env.it("works with constructors when using callThrough spy strategy", function() {
function MyClass(foo) {
if (!(this instanceof MyClass)) throw new Error('You must use the new keyword.');
this.foo = foo;
}
var subject = { MyClass: MyClass };
var spy = env.spyOn(subject, 'MyClass').and.callThrough();
expect(function() {
var result = new spy('hello world');
expect(result instanceof MyClass).toBeTruthy();
expect(result.foo).toEqual('hello world');
}).not.toThrow();
expect(function() {
spy('hello world');
}).toThrowError('You must use the new keyword.');
});
env.execute();
});