Search code examples
javascriptunit-testingmocha.jshyperledger-composer

Why test result of a transaction in mocha is wrong but it work correctly in REST test?


I write a transaction in hyperledger-fabric and implement a test unit for it on the mocha. The transaction should set the title property of an asset. After I update with assetRegistry.updade function, asset title value doesn't change but when I test in REST it works correctly.

CTO file

transaction TestOnChat {
  --> Chat chat
}

asset Chat identified by chatId {
  o String chatId
  o String title
  o DateTime createAt
  o ChatType type
  o Member[] memberList
  o Message[] messageList
}

permissions.asl

rule Test{
    description: "Member can expel other member chat"
    participant: "org.miluxas.chatnet2.User"
    operation: ALL
    resource: "org.miluxas.chatnet2.TestOnChat"
    action: ALLOW
}

logic.js

/**
 * 
 * @param {org.miluxas.chatnet2.TestOnChat} testOnChat - the teropooo
 * @transaction
 */
async function testOnChat(ex){

    ex.chat.title='fdfdfdf';
    const memberRegistry = await getAssetRegistry('org.miluxas.chatnet2.Chat');
    await memberRegistry.update(ex.chat);
}

test/logic.js

it('test chat', async () => {
    // Use the identity for Solivan.
    await useIdentity(solivanCardName);

    await createNewChat('32556','first solivan group chat','PUBLIC_GROUP');

    // Get the asset. and check if Ferzin added to chat
    const assetRegistry = await businessNetworkConnection.getAssetRegistry('org.miluxas.chatnet2.Chat');
    const asset1 = await assetRegistry.get('32556');

    // Submit add other user to chat transaction
    const transaction33 = factory.newTransaction(namespace, 'TestOnChat');
    transaction33.chat = factory.newRelationship(namespace, 'Chat', '32556');
    await businessNetworkConnection.submitTransaction(transaction33);

    //console.log(asset1.type);
    asset1.title.should.equal('fdfdfdf');
});

Solution

  • I found my mistake in test/logic.js. I had set asset1 before submitting the TestOnChat transaction and checked it after the transaction is submitted.