如何用打字稿检查锡南语中的http namedWith

问题描述 投票:0回答:1

我们正在迁移到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存根。

angular typescript unit-testing mocha sinon
1个回答
0
投票

乐于分享解决方案:

it('should stub http'), () => {
    let httpStub = sandbo.stub(TestBed.get(HttpClient), 'post').returns('Whatever');

    let result = service.PostSomething();

    httpStub.calledWith('YourPostURL').should.be.true;
}
© www.soinside.com 2019 - 2024. All rights reserved.