TypeScript,Jasmine间谍如何在callFake上调用回调?

时间:2018-12-03 13:29:33

标签: angular jasmine mocking stub spy

所以我尝试测试以下打字稿方法:

public addBet(betDTO: BetDTO) {
  if (!this.checkStake(betDTO.getStake())) {
    console.log("stake is not a number");
    return;
  }

  this.contractComService.sendDataToContract(BET_BANK_JSON, (deployedContract, RAW_TRANSACTION) => {
      deployedContract.addNewBet
        .sendTransaction(
          betDTO.getTemp(),
          this.contractComService.etherToWei(betDTO.getStake()),
          RAW_TRANSACTION
        )
        .then(data => {
          this.onSucces();
        })
        .catch(error => {
          console.log(error);
          this.onFail();
        });
    },
    betDTO.getStake()
  );
}

我必须对contractComService.sendDataToContract进行存根处理,因为它不应进行网络调用。 但是然后我必须调用通过sendDataToContract函数调用给出的回调函数。所以我希望创建间谍的代码看起来像这样:

let contractComServiceMock = jasmine.createSpyObj('ContractComService', ['sendDataToContract']);

contractComServiceMock.sendDataToContract.and.callFake((functor) => {
  console.log('fake');
  console.log(functor);
  //functor('deployed', 'trans');
});

但是函子参数从未填充?如何在测试间谍中调用回调函数(函数器)?如何填充参数?还是应该以其他方式进行测试?

编辑:我当前遇到以下错误:

TypeError: undefined is not an object (near '...)), RAW_TRANSACTION).then(function (data...') (line 69)
        addBet

在调试器中启动测试后,我也发现此错误:TypeError: Cannot read property 'then' of undefined

当前测试代码:

class BetBankMock {
  constructor(private addNewBet: ContractMethodMock) {}
}

class ContractMethodMock {
  public sendTransaction(str: string, functor): void {

  }

  public call(): void {

  }
}

class RouterMock {
  navigateByUrl(url: string): string {
    return url;
  }
}

class MatSnackBarMock {
  open(str: string, str2: string, obj: Object) {

  }
}

describe('BetsService', () => {

  let contractComServiceMock;
  let betBankMock;

  beforeEach(() => {
    contractComServiceMock = jasmine.createSpyObj('ContractComService', ['sendDataToContract', 'etherToWei', 'weiToEther']);
    let contractMethodMock = jasmine.createSpyObj('ContractMethodMock', ['call', 'sendTransaction']);
    betBankMock = new BetBankMock(contractMethodMock);

    contractComServiceMock.etherToWei.and.callThrough();
    contractComServiceMock.sendDataToContract.and.callFake((BET_BANK_JSON, functor) => {
      console.log('fake');
      console.log('functor: ', functor);
      functor(betBankMock, {});
    });


    TestBed.configureTestingModule({
      providers: [
        BetsService,
        {provide: ContractComService, useValue: contractComServiceMock},
        {provide: Router, useClass: RouterMock},
        {provide: MatSnackBar, useClass: MatSnackBarMock},
      ],
    }).compileComponents();

  });

  it('Should add a bet', () => {
    console.log(contractComServiceMock.sendDataToContract);
    console.log('getting testbet');
    let betsService = TestBed.get(BetsService);
    console.log('betservice from testbed: ', betsService);
    let bet = new BetDTO(new Date(), 1500000, DownfallType.NONE, 2, 'Amersfoort', 2000000, 'Name', 1);
    console.log('bet: ', bet);
    console.log('stake: ', bet.getStake());
    betsService.addBet(bet);

  });

});

注意:BetsService是一种依赖ContractComService的服务

0 个答案:

没有答案