Skip to content

Instantly share code, notes, and snippets.

@javaguirre
Last active January 17, 2019 16:10
Show Gist options
  • Save javaguirre/3d6c88ea463259b5582950dada0eee69 to your computer and use it in GitHub Desktop.
Save javaguirre/3d6c88ea463259b5582950dada0eee69 to your computer and use it in GitHub Desktop.
Test for mychaincode.go example on Hyperledger Fabric
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