TypeError:尝试包装已经包装的getRepository

时间:2018-05-24 06:22:05

标签: javascript typescript sinon

我需要使用sinon多次包装一个方法,以便能够根据参数返回不同的对象。我该怎么做?

我想测试的控制器看起来像这样:

const servicePackagesOfferingRepository = Salesforce.getRepository(ServicePackageOffering);
const servicePackageOffering = await servicePackagesOfferingRepository.findOneById(model.servicePackageOfferingId);

const serviceCallRepository = Salesforce.getRepository(ServiceCall);
serviceCall = await serviceCallRepository.save(serviceCall);

单位测试的是这样的:

const servicePackagesOfferingRepository = new Repository(ServicePackageOffering);
const servicePackagesOfferingRepositoryMock = sandbox.mock(servicePackagesOfferingRepository);

servicePackagesOfferingRepositoryMock
.expects('findOneById')
.withArgs(inputModel.servicePackageOfferingId)
.resolves({});

sandbox
.mock(Salesforce)
.expects('getRepository')
.withArgs(ServicePackageOffering)
.returns(servicePackagesOfferingRepository);

const serviceCallRepository = new Repository(ServiceCall);
const serviceCallRepositoryMock = sandbox.mock(serviceCallRepository);

serviceCallRepositoryMock
.expects('save')
.once();

sandbox
.mock(Salesforce)
.expects('getRepository')
.withExactArgs(ServiceCall)
.returns(serviceCallRepository);

但是我收到了这个错误:

TypeError: Attempted to wrap getRepository which is already wrapped

1 个答案:

答案 0 :(得分:0)

找到了一种方法:

const SalesforceMock = sandbox.mock(Salesforce);
SalesforceMock.expects('getRepository')
    .twice()
    .onFirstCall()
    .returns(serviceCallRepository)
    .onSecondCall()
    .returns(clientRepository);