From what I've seen, Fabric doesn't support testing private data collections with MockStub
. What I've tried to do below is workaround the lack of a transient
field on MockStub
by embedding MockStub
within another struct.
The problem is that in the chaincode, while I can extract the transient data (stub.GetTransient()
), I cannot access the needed field of my map.
Here's my struct:
type MockStubWithTransient struct {
shim.MockStub
transient map[string]interface{}
}
The test:
func TestCreatePOI (t *testing.T) {
fmt.Println("Entering TestCreatePOI")
stub := NewMockStubWithTransient("{\"poi\":{\"status\":\"unconfirmed\",\"id\":\"123\"}}")
fmt.Println(stub)
// prints: &{{[] 0x158ffc0 mockStub map[] 0xc00014e960 map[] <nil> <nil> map[] map[] 0xc0000467e0 map[]} map[poi:map[status:unconfirmed id:123]]}
// can access stub.transient["poi"]
result := stub.MockInvoke("001",[][]byte{[]byte("CreatePOI")})
fmt.Println(result)
}
func NewMockStubWithTransient(transient string) *MockStubWithTransient {
var tMap map[string]interface{}
_ := json.Unmarshal([]byte(transient), &tMap)
s := shim.NewMockStub("mockStub", new(POIChaincode))
st := &MockStubWithTransient{
transient: tMap,
MockStub: *s,
}
return st
}
When I print the stub
as my chaincode sees it, I get:
&{[[67 114 101 97 116 101 80 114 111 111 102 79 102 73 110 115 117 114 97 110 99 101]] 0x158ffc0 mockStub map[] 0xc00014e960 map[] 001 seconds:1551988379 nanos:68249460 0xc0000c6190 map[] map[] 0xc0000467e0 map[]}
And I can call stub.GetTransient()
but what I get is map[]
.
How can I populate and access the transient field of stub
when stub
originates in a test?
I'm not set on this method of doing things, I just need a way to unit test my chaincode that utilizes private data collections. Thank you!