Last active
January 17, 2019 16:10
-
-
Save javaguirre/3d6c88ea463259b5582950dada0eee69 to your computer and use it in GitHub Desktop.
Test for mychaincode.go example on Hyperledger Fabric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"strings" | |
"testing" | |
"github.com/hyperledger/fabric/core/chaincode/shim" | |
"github.com/stretchr/testify/assert" | |
) | |
func Test_GetAssetInvalidID(t *testing.T) { | |
// Arrange | |
scc := new(SmartContract) | |
stub := shim.NewMockStub("", scc) | |
// Act | |
res := stub.MockInvoke( | |
"1", | |
[][]byte{ | |
[]byte("getAsset"), | |
[]byte("1")}, | |
) | |
// Assert | |
assert.Equal(t, shim.ERROR, int(res.Status)) | |
} | |
func Test_GetAssetValidID(t *testing.T) { | |
// Arrange | |
scc := new(SmartContract) | |
stub := shim.NewMockStub("", scc) | |
asset := Asset{} | |
asset.Name = "john" | |
assetAsBytes, _ := json.Marshal(asset) | |
stub.MockTransactionStart("CREATEASSET") | |
stub.PutState("A1", assetAsBytes) | |
// Act | |
res := stub.MockInvoke( | |
"1", | |
[][]byte{ | |
[]byte("getAsset"), | |
[]byte("A1")}, | |
) | |
// Assert | |
assert.Equal( | |
t, | |
strings.Contains(string(res.Payload), "\"name\":\"John\""), | |
true, | |
) | |
assert.Equal(t, shim.OK, int(res.Status)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment