• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript Sinon.SinonSandbox类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中Sinon.SinonSandbox的典型用法代码示例。如果您正苦于以下问题:TypeScript SinonSandbox类的具体用法?TypeScript SinonSandbox怎么用?TypeScript SinonSandbox使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了SinonSandbox类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: describe

describe('modules/transactions', () => {

  let instance: TransactionsModule;
  let container: Container;
  let sandbox: SinonSandbox;

  let accountsModuleStub: AccountsModuleStub;
  let loggerStub: LoggerStub;
  let dbHelperStub: DbStub;
  let genesisBlock: SignedAndChainedBlockType;
  let transactionPoolStub: TransactionPoolStub;
  let transactionLogicStub: TransactionLogicStub;

  beforeEach(() => {
    sandbox              = sinon.createSandbox();
    container = createContainer();
    container.rebind(Symbols.modules.transactions).to(TransactionsModule);
    instance             = container.get(Symbols.modules.transactions);
    accountsModuleStub   = container.get(Symbols.modules.accounts);
    loggerStub           = container.get(Symbols.helpers.logger);
    dbHelperStub           = container.get(Symbols.helpers.db);
    genesisBlock         = container.get(Symbols.generic.genesisBlock);
    transactionPoolStub  = container.get(Symbols.logic.transactionPool);
    transactionLogicStub = container.get(Symbols.logic.transaction);

    // Reset all stubs
    [accountsModuleStub, loggerStub, dbHelperStub, transactionPoolStub, transactionLogicStub].forEach((stub: any) => {
      if (typeof stub.reset !== 'undefined') {
        stub.reset();
      }
      if (typeof stub.stubReset !== 'undefined') {
        stub.stubReset();
      }
    });
    transactionPoolStub.unconfirmed.reset();
    transactionPoolStub.multisignature.reset();
    transactionPoolStub.bundled.reset();
  });

  afterEach(() => {
    sandbox.restore();
  });

  describe('cleanup', () => {
    it('should resolve', async () => {
      await expect(instance.cleanup()).to.be.fulfilled;
    });
  });

  describe('transactionInPool', () => {
    it('should call txPool.transactionInPool and return', () => {
      transactionPoolStub.stubs.transactionInPool.returns(true);
      const retVal = instance.transactionInPool('testTxId');
      expect(transactionPoolStub.stubs.transactionInPool.calledOnce).to.be.true;
      expect(transactionPoolStub.stubs.transactionInPool.firstCall.args.length).to.be.equal(1);
      expect(transactionPoolStub.stubs.transactionInPool.firstCall.args[0]).to.be.equal('testTxId');
      expect(retVal).to.be.true;
    });
  });

  describe('getUnconfirmedTransaction', () => {
    it('should call txPool.unconfirmed.get and return', () => {
      const returnedTx = { test: 'tx' };
      transactionPoolStub.unconfirmed.stubs.get.returns(returnedTx);
      const retVal = instance.getUnconfirmedTransaction('testTxId');
      expect(transactionPoolStub.unconfirmed.stubs.get.calledOnce).to.be.true;
      expect(transactionPoolStub.unconfirmed.stubs.get.firstCall.args.length).to.be.equal(1);
      expect(transactionPoolStub.unconfirmed.stubs.get.firstCall.args[0]).to.be.equal('testTxId');
      expect(retVal).to.be.deep.equal(returnedTx);
    });
  });

  describe('getQueuedTransaction', () => {
    it('should call txPool.queued.get and return', () => {
      const returnedTx = { test: 'tx' };
      transactionPoolStub.queued.stubs.get.returns(returnedTx);
      const retVal = instance.getQueuedTransaction('testTxId');
      expect(transactionPoolStub.queued.stubs.get.calledOnce).to.be.true;
      expect(transactionPoolStub.queued.stubs.get.firstCall.args.length).to.be.equal(1);
      expect(transactionPoolStub.queued.stubs.get.firstCall.args[0]).to.be.equal('testTxId');
      expect(retVal).to.be.deep.equal(returnedTx);
    });
  });

  describe('getQueuedTransaction', () => {
    it('should call txPool.queued.get and return', () => {
      it('should call txPool.unconfirmed.get and return', () => {
        const returnedTx = { test: 'tx' };
        transactionPoolStub.queued.stubs.get.returns(returnedTx);
        const retVal = instance.getQueuedTransaction('testTxId');
        expect(transactionPoolStub.queued.stubs.get.calledOnce).to.be.true;
        expect(transactionPoolStub.queued.stubs.get.firstCall.args.length).to.be.equal(1);
        expect(transactionPoolStub.queued.stubs.get.firstCall.args[0]).to.be.equal('testTxId');
        expect(retVal).to.be.deep.equal(returnedTx);
      });
    });
  });

  describe('getMultisignatureTransaction', () => {
    it('should call txPool.multisignature.get and return', () => {
//.........这里部分代码省略.........
开发者ID:RiseVision,项目名称:rise-node,代码行数:101,代码来源:transactions.spec.ts


示例2: describe

describe('src/modules/transport.ts', () => {

  let inst: TransportModule;
  let container: Container;
  let sandbox: SinonSandbox;
  const appConfig = {
    peers: { options: { timeout: 1000, }, },
  };

  before(() => {
    sandbox = sinon.createSandbox();
  });

  beforeEach(() => {
    container = createContainer();
    container.rebind(Symbols.generic.appConfig).toConstantValue(appConfig);
    container.rebind(Symbols.modules.transport).to(proxyTransportModule.TransportModule);
  });

  let constants;
  let io: SocketIOStub;
  let schemaStub: ZSchemaStub;
  let balancesSequence: SequenceStub;
  let jobsQueue: JobsQueueStub;
  let logger: LoggerStub;
  let appState: AppStateStub;
  let broadcasterLogic: BroadcasterLogicStub;
  let transactionLogic: TransactionLogicStub;
  let peersLogic: PeersLogicStub;
  let peersModule: PeersModuleStub;
  let multisigModule: MultisignaturesModuleStub;
  let transactionModule: TransactionsModuleStub;
  let systemModule: SystemModuleStub;

  let postConstrA: AppStateStub;
  beforeEach(() => {
    io         = container.get(Symbols.generic.socketIO);
    schemaStub = container.get(Symbols.generic.zschema);

    constants        = container.get(Symbols.helpers.constants);
    balancesSequence = container.getTagged(Symbols.helpers.sequence,
      Symbols.helpers.sequence, Symbols.tags.helpers.balancesSequence);
    jobsQueue        = container.get(Symbols.helpers.jobsQueue);
    logger           = container.get(Symbols.helpers.logger);

    appState         = container.get(Symbols.logic.appState);
    broadcasterLogic = container.get(Symbols.logic.broadcaster);
    transactionLogic = container.get(Symbols.logic.transaction);
    peersLogic       = container.get(Symbols.logic.peers);

    peersModule       = container.get(Symbols.modules.peers);
    multisigModule    = container.get(Symbols.modules.multisignatures);
    transactionModule = container.get(Symbols.modules.transactions);
    systemModule      = container.get(Symbols.modules.system);

    // set appState.setComputed call for @postConstruct method
    postConstrA = new AppStateStub();
    postConstrA.enqueueResponse('get', 5);
    postConstrA.enqueueResponse('get', 5);
    appState.enqueueResponse('setComputed', true);
    appState.stubs.setComputed.callsArgWith(1, postConstrA);

    inst = container.get(Symbols.modules.transport);

    (inst as  any).sequence = {
      addAndPromise: sandbox.spy((w) => Promise.resolve(w())),
    };
  });

  afterEach(() => {
    sandbox.restore();
  });

  describe('postConstructor', () => {

    let instPostConstr: TransportModule;

    beforeEach(() => {
      postConstrA = new AppStateStub();
    });

    it('should call appState.setComputed', () => {
      postConstrA.enqueueResponse('get', 5);
      postConstrA.enqueueResponse('get', 5);
      appState.reset();
      appState.stubs.setComputed.callsArgWith(1, postConstrA);
      appState.enqueueResponse('setComputed', true);
      instPostConstr = container.get(Symbols.modules.transport);

      expect(appState.stubs.setComputed.calledOnce).to.be.true;
      expect(appState.stubs.setComputed.firstCall.args.length).to.be.equal(2);
      expect(appState.stubs.setComputed.firstCall.args[0]).to.be.equal('node.poorConsensus');
      expect(appState.stubs.setComputed.firstCall.args[1]).to.be.a('function');
    });

    it('should call IAppState.get twice', () => {
      postConstrA.enqueueResponse('get', 5);
      postConstrA.enqueueResponse('get', 5);
      appState.reset();
      appState.stubs.setComputed.callsArgWith(1, postConstrA);
//.........这里部分代码省略.........
开发者ID:RiseVision,项目名称:rise-node,代码行数:101,代码来源:transport.spec.ts


示例3: describe

describe('apis/requests/PostBlocksRequest', () => {
  let options;
  let instance: PostBlocksRequest;
  let pbHelperStub: ProtoBufHelperStub;
  let blocksModelStub: any;
  let sandbox: SinonSandbox;

  beforeEach(() => {
    const container = createContainer();
    options = {data: {block: 'b1'}};
    sandbox = sinon.createSandbox();
    instance = new PostBlocksRequest();
    instance.options = options;
    pbHelperStub = container.get(Symbols.helpers.protoBuf);
    (instance as any).protoBufHelper = pbHelperStub;
    blocksModelStub = {toStringBlockType: sandbox.stub().callsFake((a) => a) };
    (instance as any).blocksModel = blocksModelStub;
    (instance as any).generateBytesBlock = sandbox.stub().callsFake((a) => a);
    pbHelperStub.enqueueResponse('validate', true);
    pbHelperStub.enqueueResponse('encode', 'encodedValue');
  });

  afterEach(() => {
    sandbox.restore();
  });

  describe('getRequestOptions', () => {
    describe('protoBuf = false', () => {
      it('should return request options as json', () => {
        const reqOpts = instance.getRequestOptions(false);
        expect(reqOpts).to.be.deep.equal({
          data: { block: 'b1' },
          isProtoBuf: false,
          method: 'POST',
          url: '/peer/blocks',
        });
      });
    });
    describe('protoBuf = true', () => {
      it('should call protoBufHelper.validate', () => {
        instance.getRequestOptions(true);
        expect(pbHelperStub.stubs.validate.calledOnce)
          .to.be.true;
        expect(pbHelperStub.stubs.validate.firstCall.args)
          .to.be.deep.equal([options.data, 'transportBlocks', 'transportBlock']);
      });

      it('should call protoBufHelper.encode if validate is true', () => {
        instance.getRequestOptions(true);
        expect(pbHelperStub.stubs.encode.calledOnce)
          .to.be.true;
        expect(pbHelperStub.stubs.encode.firstCall.args)
          .to.be.deep.equal([options.data, 'transportBlocks', 'transportBlock']);
      });

      it('should return from protoBufHelper.encode into .data if validate is true', () => {
        const val = instance.getRequestOptions(true);
        expect(val.data).to.be.equal('encodedValue');
      });

      it('should throw if validate is false', () => {
        pbHelperStub.stubs.validate.returns(false);
        expect(() => { instance.getRequestOptions(true); }).to.throw('Failed to encode ProtoBuf');
      });
    });
  });

  describe('getBaseUrl', () => {
    describe('protoBuf = false', () => {
      it('should return the right URL', () => {
        const url = (instance as any).getBaseUrl(false);
        expect(url).to.be.equal('/peer/blocks');
      });
    });
    describe('protoBuf = true', () => {
      it('should return the right URL', () => {
        const url = (instance as any).getBaseUrl(true);
        expect(url).to.be.equal('/v2/peer/blocks');
      });
    });
  });

  describe('getResponseData', () => {
    let supportsStub: SinonStub;
    let decodeStub: SinonStub;
    beforeEach(() => {
      supportsStub = sandbox.stub();
      decodeStub = sandbox.stub().returns('decodedValue');
      (instance as any).peerSupportsProtoBuf = supportsStub;
      (instance as any).decodeProtoBufResponse = decodeStub;
    });

    describe('protoBuf = false', () => {
      it('should return the response body', () => {
        supportsStub.returns(false);
        const ret = instance.getResponseData({body: 'responseBody'});
        expect(ret).to.be.equal('responseBody');
      });
    });
    describe('protoBuf = true', () => {
//.........这里部分代码省略.........
开发者ID:RiseVision,项目名称:rise-node,代码行数:101,代码来源:PostBlocksRequest.spec.ts


示例4: describe

describe('modules/accounts', () => {
  let sandbox: SinonSandbox;
  let accountLogicStub: AccountLogicStub;
  let accountModule: IAccountsModule;
  let container: Container;

  beforeEach(() => {
    sandbox          = sinon.createSandbox();
    container        = createContainer();
    accountLogicStub = container.get(Symbols.logic.account);
    container.rebind<IAccountsModule>(Symbols.modules.accounts).to(AccountsModule).inSingletonScope();
    accountModule = container.get<any>(Symbols.modules.accounts);
  });

  afterEach(() => {
    sandbox.restore();
  });

  describe('cleanup', () => {
    it('should return resolved promise', async () => {
      await expect(accountModule.cleanup()).to.be.fulfilled;
    });
  });

  describe('.getAccount', () => {
    it('should call accountLogic.get', async () => {
      accountLogicStub.enqueueResponse('get', 'meow');
      await accountModule.getAccount({address: '1L'});
      expect(accountLogicStub.stubs.get.called).is.true;
    });
    it('should derive address from publicKey if not provided', async () => {
      accountLogicStub.enqueueResponse('generateAddressByPublicKey', '123L');
      accountLogicStub.enqueueResponse('get', 'result');
      await accountModule.getAccount({publicKey: Buffer.from('1235', 'hex')});

      expect(accountLogicStub.stubs.generateAddressByPublicKey.called).is.true;
      expect(accountLogicStub.stubs.generateAddressByPublicKey.firstCall.args[0]).is.deep.eq(Buffer.from('1235', 'hex'));
      expect(accountLogicStub.stubs.get.called).is.true;
      expect(accountLogicStub.stubs.get.firstCall.args[0]).to.be.deep.eq({
        address: '123L',
      });
    });
    it('shoul return what accountLogic.get returns', async () => {
      accountLogicStub.enqueueResponse('get', 'result');
      expect(await accountModule.getAccount({address: '123L'}))
        .to.be.eq('result');
    });
  });

  describe('.getAccounts', () => {
    it('should directly pass params to accountLogic.getAll', async () => {
      accountLogicStub.enqueueResponse('getAll', null);
      await accountModule.getAccounts({address: '1L'}, ['address']);

      expect(accountLogicStub.stubs.getAll.called).is.true;
      expect(accountLogicStub.stubs.getAll.firstCall.args.length).is.eq(2);
      expect(accountLogicStub.stubs.getAll.firstCall.args[0])
        .to.be.deep.eq({address: '1L'});
      expect(accountLogicStub.stubs.getAll.firstCall.args[1])
        .to.be.deep.eq(['address']);
    });
    it('should return what accountLogic.getAll returns', async () => {
      const theRes = {the: 'result'};
      accountLogicStub.enqueueResponse('getAll', theRes);
      const res = await accountModule.getAccounts({address: '1L'}, ['address']);

      expect(res).to.be.deep.eq(theRes);
    });
  });

  describe('.setAccountAndGet', () => {
    it('should throw if no publicKey and address is provided', async () => {
      await expect(accountModule.setAccountAndGet({} as any))
        .to.be.rejectedWith('Missing address and public key');
    });
    it('should derive address from publicKey if not provided', async () => {
      accountLogicStub.enqueueResponse('generateAddressByPublicKey', '1L');
      accountLogicStub.enqueueResponse('set', null);
      accountLogicStub.enqueueResponse('get', null);
      await accountModule.setAccountAndGet({publicKey: Buffer.from('public')});
      expect(accountLogicStub.stubs.generateAddressByPublicKey.called).is.true;
      expect(accountLogicStub.stubs.generateAddressByPublicKey.firstCall.args[0])
        .to.be.deep.eq(Buffer.from('public'));
    });
    it('should call accountLogic.set with address and data', async () => {
      accountLogicStub.enqueueResponse('set', null);
      accountLogicStub.enqueueResponse('get', null);
      await accountModule.setAccountAndGet({address: '1L'});

      expect(accountLogicStub.stubs.set.calledOnce).is.true;
      expect(accountLogicStub.stubs.get.calledOnce).is.true;

      expect(accountLogicStub.stubs.set.firstCall.args[0]).to.be.eq('1L');
      expect(accountLogicStub.stubs.set.firstCall.args[1]).to.be.deep.eq({});

      expect(accountLogicStub.stubs.get.firstCall.args[0]).to.be.deep.eq({address: '1L'});
    });
    it('should accountLogi.get with address and return its value', async () => {
      accountLogicStub.enqueueResponse('set', null);
      accountLogicStub.enqueueResponse('get', 'antani');
//.........这里部分代码省略.........
开发者ID:RiseVision,项目名称:rise-node,代码行数:101,代码来源:accounts.spec.ts


示例5: describe

describe("bst-test", function() {

    let globalModule = {
        Global: {
            initializeCLI: sinon.spy(async function () {

            }),
            config: function () {
                return { sourceID: () => "mySource" };
            },
            running : function() {
                let p = new BSTProcess();
                p.port = 9999;
                return p;
            },

            version: function () {
                return "0.0.0";
            }
        }
    };

    let sandbox: SinonSandbox = null;
    beforeEach(function () {
        mockery.enable({useCleanCache: true});
        mockery.warnOnUnregistered(false);
        mockery.warnOnReplace(false);
        mockery.registerMock("../lib/core/global", globalModule);
        sandbox = sinon.sandbox.create();
        globalModule.Global.initializeCLI.reset();
    });

    afterEach(function () {
        sandbox.restore();
        mockery.deregisterAll();
        mockery.disable();
    });

    describe("test command", function() {
        it("call initializeCLI with false", function() {
            return new Promise((resolve, reject) => {

                process.argv = command("node bst-test.js");
                mockery.registerMock("skill-testing-ml", {
                        CLI: function() {
                            return {
                                run: async function () {
                                }
                            };
                        },
                        ConfigurationKeys: [
                            {
                                key: "platform",
                                text: "Set platform"
                            },
                            {
                                key: "type",
                                text: "Set type"
                            }
                        ]
                });
                NodeUtil.load("../../bin/bst-test.js");
                assert.equal(globalModule.Global.initializeCLI.getCall(0).args[0], false);
                resolve();
            });
        });

        it("call with parameters", function() {
            return new Promise((resolve, reject) => {
                const mockRun = function(a, b){
                    assert.equal(b.platform, "google");
                    resolve();
                };
                const mockCli = function() {
                    return {
                        run: mockRun
                    };
                };
                const skillTestingMock = {
                    CLI: mockCli,
                    ConfigurationKeys: [
                        {
                            key: "platform",
                            text: "Set platform"
                        },
                        {
                            key: "type",
                            text: "Set type"
                        }
                    ]
                };
                mockery.registerMock("skill-testing-ml", skillTestingMock);
                process.argv = command("node bst-test.js --platform google");
                NodeUtil.load("../../bin/bst-test.js");
                assert.equal(globalModule.Global.initializeCLI.getCall(0).args[0], false);
            });
        });
    });
});
开发者ID:bespoken,项目名称:bst,代码行数:99,代码来源:bst-test-test.ts


示例6: oldEncode

 msg.encode      = (pl) => {
   const m   = oldEncode(pl);
   finishSpy = sandbox.spy(m, 'finish');
   return m;
 };
开发者ID:RiseVision,项目名称:rise-node,代码行数:5,代码来源:protobuf.spec.ts


示例7: it

 it('should call getMessageInstance', () => {
   getMsgInstSpy = sandbox.spy(instance as any, 'getMessageInstance');
   instance.decode(buf, 'APISuccess');
   expect(getMsgInstSpy.calledOnce).to.be.true;
   expect(getMsgInstSpy.firstCall.args).to.be.deep.equal(['APISuccess', undefined]);
 });
开发者ID:RiseVision,项目名称:rise-node,代码行数:6,代码来源:protobuf.spec.ts


示例8:

 (instance as any).getMessageInstance = (namespace, messageType) => {
   msg        = (instance2 as any).getMessageInstance(namespace, messageType);
   verifyStub = sandbox.stub(msg, 'verify').returns('Test Err');
   return msg;
 };
开发者ID:RiseVision,项目名称:rise-node,代码行数:5,代码来源:protobuf.spec.ts


示例9:

test.afterEach(function () {
  sandbox.restore();
});
开发者ID:OneSignal,项目名称:OneSignal-Website-SDK,代码行数:3,代码来源:CustomLink.ts


示例10: afterEach

 afterEach(function () {
     sandbox.restore();
     mockery.deregisterAll();
     mockery.disable();
 });
开发者ID:bespoken,项目名称:bst,代码行数:5,代码来源:bst-intend-test.ts



注:本文中的Sinon.SinonSandbox类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript Sinon.SinonSpy类代码示例发布时间:2022-05-25
下一篇:
TypeScript Sinon.SinonMock类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap