Testing the Invoke method
It's now time to define the test for the Invoke function. In line 7 of the following codeblock, checkInit is called to initialize the ledger, followed by checkInvoke in line 13, which invokes the requestTrade function. The requestTrade function creates a new trade asset and stores it on the ledger. To verify that the ledger contains the correct state, a new TradeAgreement is created and serialized in lines 15 and 16, before a new composite key is calculated in line 17. Finally, in line 18, the state of the key is verified against the serialized value.
Additionally, as previously outlined, our chaincode contains a series of functions that together define the trade workflow. We will chain the invocations of these functions into a sequence in the test to verify the whole workflow. The code of the whole function is available in the test file located in the chaincode folder.
func TestTradeWorkflow_Agreement(t *testing.T) { scc := new(TradeWorkflowChaincode) scc.testMode = true stub := shim.NewMockStub("Trade Workflow", scc) // Init checkInit(t, stub, getInitArguments()) // Invoke 'requestTrade' tradeID := "2ks89j9" amount := 50000 descGoods := "Wood for Toys" checkInvoke(t, stub, [][]byte{[]byte("requestTrade"), []byte(tradeID), []byte(strconv.Itoa(amount)), []byte(descGoods)}) tradeAgreement := &TradeAgreement{amount, descGoods, REQUESTED, 0} tradeAgreementBytes, _ := json.Marshal(tradeAgreement) tradeKey, _ := stub.CreateCompositeKey("Trade", []string{tradeID}) checkState(t, stub, tradeKey, string(tradeAgreementBytes)) ... }
Following snippet shows the function checkInvoke .
func checkInvoke(t *testing.T, stub *shim.MockStub, args [][]byte) { res := stub.MockInvoke("1", args) if res.Status != shim.OK { fmt.Println("Invoke", args, "failed", string(res.Message)) t.FailNow() } }