首页 > 解决方案 > 如何使用 ChainCodeMockStub 测试 nodejs 链代码

问题描述

我正在尝试测试 nodejs 链代码而不将其部署到超级账本Fabic 网络。有一个流行的 nodejs 库,称为 @theledger/fabric-mock-stub。下面是我的单元测试

const Chaincode = require('./index.js');
// import { ChaincodeMockStub, Transform } from "@theledger/fabric-mock-stub";
const ChaincodeMockStub = require("@theledger/fabric-mock-stub")
// You always need your chaincode so it knows which chaincode to invoke on
const chaincode = new Chaincode();
describe('Test MyChaincode', () => {
    it("Should init without issues", async () => {
        const mockStub = new ChaincodeMockStub("MyMockStub", chaincode);
        const response = await mockStub.mockInit("tx1", []);
        expect(response.status).to.eql(200)
    });
});

运行此测试时收到以下错误
2019-04-08T18:34:55.530Z ERROR [lib/chaincode.js] uncaughtException: Missing required argument peer.address

有谁知道如何使用这个测试库?https://github.com/wearetheledger/fabric-mock-stub

任何帮助将不胜感激,谢谢。

标签: node.jstestinghyperledger-fabric

解决方案


我有同样的问题,我注意到在我的链码 js 文件的底部我有

shim.start(new Chaincode())

如果您将此行移至另一个文件或在进行测试之前将其注释掉,则测试应该可以正常工作。


推荐阅读