单元测试链式承诺
Unit testing chained promises
我如何对调用导入的 class 方法的 class 方法进行单元测试?我有以下结构:
import { SomeClass } from 'some-library';
class MyClass extends AnotherClass {
myMethod() {
const someClass = new SomeClass();
return someClass.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
我有以下测试
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
这仍然很简单,因为我不确定如何处理它。分解 then
中的代码会更好吗?
更新
显然 SomeClass
是一个单例,而 sinon 抛出一个错误说 somePromiseMethod
是一个 non-existent own property
。我更改了存根以调用其 prototype
,现在正在调用存根。
class MyClass extends AnotherClass {
myMethod() {
const someClassInstance = SomeClass.getInstance();
return someClassInstance.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
现在,从那时起,第二个 then
将只是 return data
,我可以将 //Do stuff
放在一个单独的函数中并对其进行测试。
您正在删除错误的方法 somePromiseMethod
存在于 SomeClass
的 prototype
上,因此您需要将其存根。 Sinon 应该让你做这样的事情:
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
// You may be able to remove the Promise.resolve as well, as I think resolves does this for you
.resolves({
data: [],
});
我如何对调用导入的 class 方法的 class 方法进行单元测试?我有以下结构:
import { SomeClass } from 'some-library';
class MyClass extends AnotherClass {
myMethod() {
const someClass = new SomeClass();
return someClass.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
我有以下测试
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
这仍然很简单,因为我不确定如何处理它。分解 then
中的代码会更好吗?
更新
显然 SomeClass
是一个单例,而 sinon 抛出一个错误说 somePromiseMethod
是一个 non-existent own property
。我更改了存根以调用其 prototype
,现在正在调用存根。
class MyClass extends AnotherClass {
myMethod() {
const someClassInstance = SomeClass.getInstance();
return someClassInstance.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
现在,从那时起,第二个 then
将只是 return data
,我可以将 //Do stuff
放在一个单独的函数中并对其进行测试。
您正在删除错误的方法 somePromiseMethod
存在于 SomeClass
的 prototype
上,因此您需要将其存根。 Sinon 应该让你做这样的事情:
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
// You may be able to remove the Promise.resolve as well, as I think resolves does this for you
.resolves({
data: [],
});