Merge pull request #28417 from nicojs/allow-create-stubbed-instance-with-private-constructors

sinon: Allow stubbed instance with private constructors
This commit is contained in:
Armando Aguirre
2018-08-27 13:04:11 -07:00
committed by GitHub
2 changed files with 24 additions and 14 deletions

View File

@@ -1409,9 +1409,7 @@ declare namespace Sinon {
*
* @template TType Type being stubbed.
*/
interface StubbableType<TType> {
new(...args: any[]): TType;
}
type StubbableType<TType> = Function & { prototype: TType };
/**
* An instance of a stubbed object type with functions replaced by stubs.

View File

@@ -67,14 +67,26 @@ function testSandbox() {
sb.replaceSetter(replaceMe, 'setter', (v) => { });
const cls = class {
foo() {}
foo() { }
bar: number;
};
const PrivateFoo = class {
private constructor() { }
foo() { }
bar: number;
static create() {
return new PrivateFoo();
}
};
const stubInstance = sb.createStubInstance(cls);
const privateFooStubbedInstance = sb.createStubInstance(PrivateFoo);
stubInstance.foo.calledWith('foo');
privateFooStubbedInstance.foo.calledWith('foo');
const clsFoo: sinon.SinonStub = stubInstance.foo;
const privateFooFoo: sinon.SinonStub = privateFooStubbedInstance.foo;
const clsBar: number = stubInstance.bar;
const privateFooBar: number = privateFooStubbedInstance.bar;
}
function testFakeServer() {
@@ -106,7 +118,7 @@ function testXHR() {
sinon.FakeXMLHttpRequest.useFilters = true;
sinon.FakeXMLHttpRequest.addFilter((method, url, async, user, pass) => true);
sinon.FakeXMLHttpRequest.onCreate = (xhr) => {};
sinon.FakeXMLHttpRequest.onCreate = (xhr) => { };
sinon.FakeXMLHttpRequest.restore();
}
@@ -117,7 +129,7 @@ function testClock() {
let now = 0;
now = clock.now;
const fn = () => {};
const fn = () => { };
clock.setTimeout(fn, 0);
clock.setTimeout(fn, 0, 'a', 'b');
@@ -173,7 +185,7 @@ function testExpectation() {
function testMatch() {
const obj = {};
const fn = () => {};
const fn = () => { };
sinon.match(5).test(5);
sinon.match('str').test('foo');
@@ -216,7 +228,7 @@ function testMatch() {
}
function testFake() {
const fn = () => {};
const fn = () => { };
let fake = sinon.fake();
fake = sinon.fake(() => true);
@@ -277,9 +289,9 @@ function testAssert() {
}
function testSpy() {
const fn = () => {};
const fn = () => { };
const obj = class {
foo() {}
foo() { }
};
const instance = new obj();
@@ -369,14 +381,14 @@ function testSpy() {
function testStub() {
const obj = class {
foo() {}
foo() { }
};
const instance = new obj();
let stub = sinon.stub();
stub = sinon.stub(instance, 'foo');
const spy: sinon.SinonSpy = stub;
const spy: sinon.SinonSpy = stub;
sinon.stub(instance);
@@ -408,9 +420,9 @@ function testStub() {
stub.callsArgOnAsync(1, instance);
stub.callsArgWithAsync(1, 'a', 2);
stub.callsArgOnWithAsync(1, instance, 'a', 2);
stub.callsFake(() => {});
stub.callsFake(() => { });
stub.get(() => true);
stub.set((v) => {});
stub.set((v) => { });
stub.onCall(1).returns(true);
stub.onFirstCall().resolves('foo');
stub.onSecondCall().resolves('foo');