如何使用 Typescript/Angular 检查 Sinon 中的 http calledWith
How to check http calledWith in Sinon with Typescript/Angular
我们正在迁移到 TypeScript,我们希望继续使用 Sinon 进行测试。
在我们使用 JavaScript 服务单元测试之前:
it('should get buyer application status count', function () {
let httpStub = sandbox.stub(http, 'get').returns({
then: function () {
return [];
}
});
let result = service.get();
httpStub.calledWith(`${constants.APPLICATION_BASE}ApiService/GetMethod`).should.be.true;
});
service.get()
方法对 ApiService/GetMethod
进行 http
调用,我们确保使用此确切的 URL.
调用它
我们如何在 TypeScript 中使用 Sinon?
目前我们这样做:
it('should get list', () => {
// Arrange
let apiServiceStub: SinonStubbedInstance<ApiService>;
// Act
apiServiceStub= sinon.createStubInstance(ApiService);
let result = apiServiceStub.get();
// Assert -- Here is my question, how to do this line, it doesn't work now
applicationServiceStub.**calledWith**(`${constants.APPLICATION_BASE}ApiService/GetMethod`).should.be.true;
});
就像现在所做的那样,calledWith 方法只匹配传递给 menthod 的参数,而不匹配调用 HTTP 调用的方式。
我需要可以创建类似于第一个示例的东西 - http 存根。
很高兴分享解决方案:
it('should stub http'), () => {
let httpStub = sandbo.stub(TestBed.inject(HttpClient), 'post').returns('Whatever');
let result = service.PostSomething();
httpStub.calledWith('YourPostURL').should.be.true;
}
我们正在迁移到 TypeScript,我们希望继续使用 Sinon 进行测试。
在我们使用 JavaScript 服务单元测试之前:
it('should get buyer application status count', function () {
let httpStub = sandbox.stub(http, 'get').returns({
then: function () {
return [];
}
});
let result = service.get();
httpStub.calledWith(`${constants.APPLICATION_BASE}ApiService/GetMethod`).should.be.true;
});
service.get()
方法对 ApiService/GetMethod
进行 http
调用,我们确保使用此确切的 URL.
我们如何在 TypeScript 中使用 Sinon?
目前我们这样做:
it('should get list', () => {
// Arrange
let apiServiceStub: SinonStubbedInstance<ApiService>;
// Act
apiServiceStub= sinon.createStubInstance(ApiService);
let result = apiServiceStub.get();
// Assert -- Here is my question, how to do this line, it doesn't work now
applicationServiceStub.**calledWith**(`${constants.APPLICATION_BASE}ApiService/GetMethod`).should.be.true;
});
就像现在所做的那样,calledWith 方法只匹配传递给 menthod 的参数,而不匹配调用 HTTP 调用的方式。 我需要可以创建类似于第一个示例的东西 - http 存根。
很高兴分享解决方案:
it('should stub http'), () => {
let httpStub = sandbo.stub(TestBed.inject(HttpClient), 'post').returns('Whatever');
let result = service.PostSomething();
httpStub.calledWith('YourPostURL').should.be.true;
}