Testing chaincode using the shim

Let's see how to test the earlier chaincode developed locally in the Go language. Before that, here are some key points to note:

  • Install the Go language locally on your machine.
  • This test filename should take this form: <Go file name>_test.go.

For example: if the chaincode name is education.go, then this test filename should be education_test.go.

  • Keep both files in the same folder.
  • Set GOPATH to the folder.
  • Install the dependent packages used in the chaincode if they can't be found.

For example: go get github.com/hyperledger/fabric/protos/peer
go get github.com/hyperledger/fabric/core/chaincode/shim.C

  • Code snippet file education_test.go at the GIT repository (https://github.com/PacktPublishing/Oracle-Blockchain-Quick-Start-Guide), is a test case with an explanation for only one method: initReceiver(). Similarly, you can write test cases to all other methods.
  • Each test case should be prefixed with Test<function name>.
    For example: TestInitReceiver.
  • After the test cases are ready, test them using the following command:

go test -run <<function name>>

For example: go test -run Education.

Here, Education is the test case name.

To test a function, create a stub using NewMockStub(). The stub has a MockInvoke() function, which invokes an actual function of chaincode.

For example, stub.MockInvoke("001",[][]byte{[]byte("insertReceiver "), []byte(key),[]byte("Anand Yerrapati"), []byte("Blockchain")}).

Here, 001 is a transaction ID to be returned upon the success of this test and insertReceiver is a function to be invoked in the education.go file. The remaining are the parameters to be passed to the insertReceiver function.

Refer file 'education_test.go' at the GIT repository "https://github.com/PacktPublishing/Oracle-Blockchain-Quick-Start-Guide-".

Test filename to test the chaincode (education.go), is test file "education_test.go". This file is referred in this section for testing chaincode using the shim.

This test case is consolidated for flow, from inserting a receiver, querying the receiver, inserting a certificate, verifying the certificate, approving the certificate, and querying the certificate again, to verifying the changes. The following are the results of the earlier test case after executing the go test -run Education command:

D:AnandOBPchaincode	estinggo>go test -run Education
Inside TestEducation
invoke is running insertReceiver
start insert receiver
receiver:
&{receiver std1231 Anand Y Blockchain}
End init receiver
Result insertReceiver:
{200 [] {} [] 0}
invoke is running queryReceiverById
Result queryReceiverById:
&{receiver std1231 Anand Y Blockchain}
invoke is running insertCertificate
cert_receiver: Anand Y
{200 [] {} [] 0}
Result insertCertificate:
{200 [] {} [] 0}
invoke is running queryCertificateById
Result queryCertificateById:
&{certificate cert123 12345 ORU Blockchain Certificate Anand Y std1231 ORU IT 06/04/2019 06/04/2019 Blockchain course completed Active}
invoke is running approveCertificate
{200 [] {} [] 0}
Result approveCertificate:
{200 [] {} [] 0}
invoke is running queryCertificateById
Result queryCertificateById:
&{certificate cert123 12345 ORU Blockchain Certificate Anand Y std1231 ORU IT 06/04/2019 06/04/2019 10:41:50 Blockchain course completed Approved}
PASS
ok _/D_/Anand/OBP/chaincode/testing/go 3.921s
The mock stub doesn't support every function. You can't implement the GetQueryResult and GetHistoryForKey methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset