Re: Testing nodejs smart contracts without deploying to a network


Ross Tang <tangross@...>
 

Using Jest is very good option, you can easily mocking the context object of contract-api, something like.

const ctx: any = {
stub: {
createCompositeKey: jest.fn(),
deleteState: jest.fn(),
getState: jest.fn(),
putState: jest.fn(),
setEvent: jest.fn(),
getStateByPartialCompositeKey: jest.fn()
}
};
const context = {
stateList: new StateList(ctx, 'entities'),
...ctx
};

ctx.stub.createCompositeKey.mockResolvedValue('entities"en""entId""2019"');
ctx.stub.putState.mockResolvedValue(Buffer.from(''));

And, you can run unit like by directly invoke the transactions, calling the function.

describe('Chaincode Tests', () => {
it('should instantiate', async () =>
cc
.instantiate(context)
.then<any[]>((response: any) => JSON.parse(response))
.then(json =>

That save me great amount of time, in chaincode development. 

Besides, the manual mocking of Jest is very useful, to create mock database. Imagine you are using Commercial Paper example, the stateliest implementation can replaced by mocked database, in json format.

jest.mock('../ledger-api/statelist');

const context: any = {
clientIdentity: {
getMSPID: jest.fn(),
getID: jest.fn(),
getX509Certificate: jest.fn()
},

On 30 Oct 2019, at 5:17 AM, Siddharth Jain <siddjain@...> wrote:

What is the best way to test smart contracts written in nodejs using the fabric-contract-api and without having to deploy to a running network? https://github.com/wearetheledger/fabric-mock-stub seems to be geared towards smart contracts developed using the old fabric-shim API.

Join fabric@lists.hyperledger.org to automatically receive all group messages.