我们正在迁移到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()
方法对http
进行ApiService/GetMethod
调用,我们确保使用此确切的URL对其进行调用。
我们如何使用Sinon在TypeScript中实现相同?
目前,我们这样做:
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;
});
现在已完成,被调用的方法仅将传递的参数与方法匹配,而不匹配如何调用HTTP调用。我需要可以创建类似于第一个示例的内容-http存根。
乐于分享解决方案:
it('should stub http'), () => {
let httpStub = sandbo.stub(TestBed.get(HttpClient), 'post').returns('Whatever');
let result = service.PostSomething();
httpStub.calledWith('YourPostURL').should.be.true;
}